What is a thread in Java?

In this article we will talk about:

  1. What is the concept of thread in Java?
  2. What happens regarding threads when you start a JVM?

So, stay tuned!!

Current article is part of our series on multithreading in Java.

Thread In Java

Let’s observe a simple Java program.

public class AdditionExample {

   public static void main(String[] args) {
        
      int number1= 10, number2= 20, result;
      result= number1+ number2;

      System.out.println("Sum is: "+result);
   }
}

Till this point, you were writing Java programs in a similar manner as of the above example. Of course, you might be using a bit more complex logic than this.

But in nutshell, whatever code you were writing was getting executed sequentially, one after the other, through main() method.

NOW, let’s first understand the relationship of JVM and our program.

JVM And It’s Threads

When you execute your code by java command like java AdditionExample, it gets started as a JVM process. Now behind the scenes, this JVM process starts multiple threads to do it’s job.

Out of these multiple threads, only one thread executes all you main() method logic. And can you guess, what could be the name of this thread? Well, it’s called Main thread.

You must be wondering: What does other threads do when all of our code is executed by a single thread?

Well, JVM does many complicated tasks like Garbage Collection and Memory management, which alone requires multiple threads to work efficiently. Right now, hold onto the thought of other JVM threads.

Later on in our series, we will surely discuss things like:

  1. How to know the name of a thread?
  2. How to name our own custom thread?
  3. How to see the list of threads currently running? etc.

FOR NOW , just grab the concept that even when you write a simplest of Java program, it is multi-threaded behind the scenes.

Alright! But what is a thread exactly?

Defining a thread in Java

By definition, a thread is the smallest unit of execution in Java programming.

As a matter of fact, this definition is not just restricted to Java. It is applicable to other programming languages as well.

Operating systems also works in a multi-threaded ways where we for sure have heard the concept of:

  1. User threads
  2. and Kernel threads.

Even the CPUs are hyper-threaded now-a-days.

If you have not heard about these things, don’t worry, we will discuss that also, later on.

JUST KEEP IN MIND that, a thread in Java provides us an execution environment wherein we can execute our code. And all these threads lives inside a JVM process.

Key Points

  1. A thread in Java is a smallest unit of execution in which we can execute some part of our Java application.
  2. Even the simplest of Java Application is multi-threaded behind the scenes because JVM is multi-threaded.
  3. main() method gets executed in the JVM’s “Main thread”.

What to read next?

We have briefly touched upon what JVM does when it starts. Next we will be discussing the relationship between process and thread and their differences.

Go to: process vs thread

Leave a Comment