in Core Java

Thread States and sleep, join and Yield methods

Thread States

From creation of Thread Objects to their Termination, Threads transit through different states in their life time. 

When we create a new thread object, the Thread is said to be in NEW state. In this state it is just an object like any other object that you create in Java. This thread object is not a real execution engine which can execute code.
To make a thread object to execute code, we invoke start method on it. When start is invoked, The thread is said to be in Runnable state. Remember that In this state the thread is not executing any code but it is ready to execute the code in the run method.

Thread States- New, Runnable, Running, Sleep/wait/blocked and Dead

When the thread starts executing the code in the run method, it is said to be in RUNNING state.
Other than these two states, a thread can also move to Sleep/Wait/ Blocked state. When we invoke sleep method on the current thread, we are telling it to pause for specified amount of time. Apart from sleep, a thread can also move to Wait of blocked state.
When the thread is in any of these three states, it is said to be active or alive .
Finally when all the code that is in the run method is complete, the thread is Terminated and we call it a Dead Thread.
Once a Thread is dead, we cannot invoke the start method to start it again. It is like a Black hole. 

Sleep Method in Thread:

There will be some scenarios where in you want a thread to stop executing the code for a period of time and then start again.

  • Sleep method in Thread tells the currently executing thread to sleep for specified amount of time. 
  • Sleep method accepts the time in MilliSeconds.
  • It can throw interruptedException . So it should be embedded in the try catch block
  • Remember that there is no guarantee that the thread will go to Sleep state the moment is it executed and no guarantee that the thread will sleep for specified amount of time. The thread scheduler can wake it up any time.
  • Once the thread completes or out of its sleep state, it can move to Running or Runnable state.

Thread Sleep – An example

Racer.java

Thread Priorities:

Understanding the Thread priorities is important to know how yield method works in Multithreading.

  • Remember that all the threads carry normal priority when a priority is not specified.
  • Priorities can be specified from 1 to 10. 10 being the highest, 1 being the lowest priority and 5 being the normal priority.
  • Remember that the thread with highest priority will be given preference in execution. But there is no guarantee that it will be in running state the moment it starts.
  • Always the currently executing thread might have the higher priority when compared to the threads in the pool who are waiting for their chance.
  • It is the thread scheduler which decides what thread should be executed.
  • t.setPriority() can be used to set the priorities for the threads.
  • Remember that the priorities should be set before the threads start method is invoked.
  • You can use the constants, MIN_PRIORITY,MAX_PRIORITY and NORM_PRIORITY for setting priorities.

Yield Method in Thread:

  • Yield is a Static method .
  • Yield tells the currently executing thread to give a chance to the threads that have equal priority in the Thread Pool.
  • There is no guarantee that Yield will make the currently executing thread to runnable state immediately.
  • Remember an important point that yield method does not make the thread to go to Wait or Blocked state. It can only make a thread from Running State to Runnable State.

Yield Example:

Join Method in Thread:

  • Join method belongs to Thread object and not a static member. When join is invoked an a thread instance, this thread will tell currently executing thread to wait till the Joining thread completes.
  • Join is used in the situations when a task which should be completed before the current task is going to finish.
  • Just to give another example of how Join works, say we have a slow train on the track moving and another fast train should take the same track, so, we make way to the fast train and tell the slow train to join once after the fast one is left.

Join method Example:

References:

Watch Complete Video of  Thread States and Priorities

Download the Thread States PPT

java.lang.Thread

Srinivas Reddy

Srinivas Reddy

An IT - Specialist with loads of experience in Java, J2ee platforms and loves to share his experience on technology with You...

More Posts - Website

Follow Me:
TwitterFacebook

Related Posts

Srinivas Reddy
Written By:

An IT - Specialist with loads of experience in Java, J2ee platforms and loves to share his experience on technology with You...