Java Thread

Java Thread
Thread term can be used in two ways

  • An instance of class java.lang.Thread
    A Thread of Execution

An Instance of Thread is an object like any other object in Java, it contains variables and methods which lives and dies on the heap. But a Thread of Execution is an individual process which has its own stack call. Even if you don’t create any thread in your program, thread will be running there and main() method will start that.

Note: In case of thread most of thing is unpredictable.

Type of Thread
Thread can be of two types.

  1. User Thread
  2. Daemon Thread

Difference between these two type of thread is that JVM exists an application only when all User Thread are complete. JVM doesn’t care about the status of Daemon Thread.

Thread Definition
A thread can be defined in two ways

  • Extend the java.lang.Thread class.
  • Implement the Runnable interface.

Extending Java Thread class:
Class need to extend the Thread class & Override the run() method.

But the problem with this approach is that class can not extend any more class.

Note : run method can be overloaded in class. but only run() method(without argument) will be consider by JVM. Any other overridden method will needs to be called explicitly.

Implements runnnable interface
Class needs to implement runnable interface and override the run() method. Creating Thread in this way gives you flexibility to extend any class you like.

Instantiating Thread :
Every thread of execution begins as an instance of class Thread. In any case Thread objects needs to be created before creating Thread of Execution.
Creating Thread object is different for both the cases. In case of Extending Thread class you can directly initiate class with new keyword as it has extended Thread class itself.

In case of implimenting Runnable interface. First created runnable class needs to be instatiated.

Now pass this runnable object to Thread.

If you create a thread using the no-arg constructor, the thread will call its own run(). This happened in first case(Extending Thread class) But in case of Runnable Thread class needs to know that run method from class implementing runnnable interface needs to be invoked instead of run() from Thread class. So we need to pass the class as argument to Thread. Single runnable instance can be passed to multiple Thread object.

Giving the same target to multiple threads means that several threads of execution will be running the very same job (and that the same job will be done multiple times).

Note : The Thread class itself implements Runnable. (After all, it has a run() method that we were overriding.) This means that you could pass a Thread to another Thread’s constructor.

Overloaded constructor in Thread class:

Till now we have created Thread object and it knows what to do in form of run() method. But till now it is still an object. It does not have its own call stack. In other words Thread of execution has not been started. A Thread object will have its own stack only when start() method will be called on Thread.

Till now Thread has 2 states.

  • New state (Thread object created but Start method not invoked)
  • runnable(alive) state (start method is invoked )

Note : Calling run() method from java code directly mean that you are invoking a method and stack will not be created.

When a Thread is in running state means Thread’s run method is not yet executed and thread is waiting his turn. Once Thread got selected thread’s run method will execute and that state will called running state.

Thread Scheduler :
The thread scheduler is the part of the JVM that decides which thread should run at any given moment, and also takes threads out of the run state. Any thread in the runnable state can be chosen by the scheduler to be the one and only running thread. If a thread is not in a runnable state, then it cannot be chosen to be the currently running thread. Some method that can influence scheduler to some extent(Note*: We can’t control the behavior of Thread scheduler)

These methods are from Thread class

Methods from Object class.

Till now we come across 3 state of Thread

  • New
  • Runnable
  • Running

There are some more state of thread in which Thread will not be eligible to run.

  • Waiting/blocked/sleeping
  • Dead

A thread is in Dead state when its run method completed its execution.

The sleep() method is a static method of class Thread. (where it still has to beg to be the currently running thread). as it is static method only currently running Thread will sleep. Syntax would be : Thread.sleep()

yield: yield method is related with the priority of Thread. It is also static method so it works only on currently running thread. Scheduler make it sure that If a thread enters the runnable state, and it has a higher priority than any of the threads in the pool and a higher priority than the currently running thread, the lower-priority running thread usually will be bumped back to runnable and the highest-priority thread will be chosen to run. at any given time the currently running thread usually will not have a priority that is lower than any of the threads in the pool. yield() is supposed to do is make the currently running thread head back to runnable to allow other threads of the same priority to get their turn.

Join is a non static method. It lets one thread “join onto the end” of another thread. If you join Thread B to Thread A Thread B will not start untill Thread A complets.

Syntax :

takes the currently running thread and joins it to the end of the thread referenced by t

There are several other way in which Thread can leave running state and can return to Runnable state. run methods completes a call to wait()

Synchronize: It works with Lock. Lock is of two type.

Static lock

Non static lock

Note: There is only one lock per object. Once a thread got the lock of an object no other thread can enter the synchronise block/method of given object.
Only methods/block can be synchronised not variable or class.
A class can have both synchronised / non synchronised method.
Thread can access non synchronised block even if one Thread got the lock of give object.
If a thread goes to sleep, it holds any locks it has. A thread can have Lock of different object at the same time.

Syntax :


Synchronised Static method :

There is only one copy of the static data, so you only need one lock per class to synchronize static methods—a lock for the whole class. There is such a lock; every class loaded in Java has a corresponding instance of java.lang.Class representing that class. It’s that java.lang.Class instance whose lock is used to protect the static methods of the class.



MyClass.class thing is called a class literal. It tells the compiler (who tells the JVM): go and find me the instance of Class that represents the class called MyClass.

Note : Threads calling non-static synchronized methods in the same class will only block each other if they’re invoked using the same instance. As there is only one lock per instance. Threads calling static synchronized methods in the same class will always block each other— As there is only one lock per class.
A static synchronized method and a non-static synchronized method will not block each other. As they need lock for different thing(Object & class)

Note : join(), sleep(), yield() these methods keep locks. while wait release Lock.