Monday, 22 June 2015

Thread states in Java multithreading

It is important to know about the lifecycle of a thread and various states a thread can be in. That will give an idea about what happens after calling the start() method on a thread and how it is scheduled to run.

As we have already seen in a post about creating a thread that a thread can be created by extending Thread class or implementing Runnable interface, in both cases we need to provide implementation of the run() method. In order to begin execution of the run() method we need to call the start() method on the thread. That is when Java Virtual Machine calls the run method of the thread.

Once scheduled to run by JVM a thread will run when its gets CPU cycle, a thread may be in waiting, blocked or running state after scheduled to run and later transitions to terminated state. Let's see the states a thread can be in.

New

When a thread is created either by extending Thread class or implementing Runnable interface it is in "New State".

Thread thread1 = new ThreadDemoClass();

When a thread is in "New" state it is not yet scheduled to run.

Runnable

When we call start() method on the thread object that causes the thread to begin execution and it's the Java Virtual Machine that calls the run method of the thread.

Thread thread1 = new ThreadDemoClass();
thread1.start();

This state is called Runnable as thread may not start running as soon as the start() method is called. It depends on the native OS when it schedules the thread to start running. A thread starts running when it gets the CPU cycle. A running thread may change state to waiting, blocked, runnable again and terminated.

Blocked

When a resource is shared among various threads then a thread may go into blocked state as the resource may be used by another thread. In that case a thread has to suspend its execution because it is waiting to acquire a lock.
As example in case of synchronized block where only one thread can enter that block of code.

synchronized (object reference) {   
  //code that uses shared resource 
}  

A thread may also be blocked when it is blocking on I/O. As example when a thread invokes a read() or write() method on I/O stream, that thread is blocked until there is some data to read, or the data is fully written.

Waiting

A thread that is waiting indefinitely for another thread to perform a particular action is in the waiting state.
As example A thread is waiting because of a call to wait() or join() method where no time of waiting is specified as parameter.

Timed_Waiting

A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state.
As exp. A thread is waiting because of a call to wait() or join() method where time of waiting is specified as parameter. Calling sleep(long millis) will also result in a thread entering a TIMED_WAITING state.

join() method

Using join() method with a parameter where time of waiting is specified in milliseconds.

public final void join(long millis) throws InterruptedException
rather than Using join() without any parameter
public final void join() throws InterruptedException

Example with Sleep

Thread myThread = new MyThreadClass();
myThread.start();
try {
    myThread.sleep(10000);
} catch (InterruptedException e){
 
}

Terminated

A thread that has exited is in this state. This happens when the thread has completed executing the run() method. A thread may also be terminated any time by calling its stop() method (note that Thread.stop() is deprecated and it is inherently unsafe)

thread states in java multithreading
Various thread states

That's all for this topic Thread states in Java multithreading. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. Difference between Thread and Process in Java
  2. What if run() method called directly instead of start() method
  3. Can we start the same thread twice in Java
  4. Thread priorities in Java multithreading
  5. Why wait(), notify() and notifyAll() methods are in Object class
  6. Java Multi-threading interview questions

You may also like -

No comments:

Post a Comment