Java Thread Scheduler

In Java program, you create threads but they are not executed by Java alone. Java takes the help of the underlying OS to execute them. To allocate microprocessor time and to supervise all the threads' execution, the OS comes with Thread Scheduler. The entire responsibility of maintaining the sequence of execution of threads, where which thread should be given first preference than the other, lies with the thread scheduler. The scheduling depends on the algorithm of the scheduler. Many types of algorithms exist like preemptive and time slicing with round robin etc. It is a very complex algorithm that executes many times in a given time. The scheduler maintains a pool of threads. When Java thread is started calling start() method, it joins the pool of waiting threads. For deciding processor allocation for each waiting thread, the scheduler takes many aspects into consideration.

  • Priority of thread
  • Waiting time of thread
  • Nature of thread
The JVM is based on Preemptive and priority based scheduling algorithm. The thread with more priority is given first preference than the thread with less priority. The thread with more priority relinquishes (empties) the thread with less priority that is being executed. If the threads of equal priority are in the pool, the waiting time is taken in consideration. Nature of threads sometimes affects. The daemon threads are given less importance and are executed only when no other thread is available for execution.

Life Cycle Of A Thread

Thread Life Cycle

  • New: A new thread begins its life cycle in the new state. It remains in this state until the program starts the thread. It is also referred to as a born stage of a thread.
  • Runnable: After a newly born thread is started, the thread becomes runnable. A thread in this state is considered to be executing its task.
  • Running: A thread is in running state that means the thread is currently executing. There are several ways to enter in Runnable state but there is only one way to enter in Running state: the scheduler select a thread from runnable pool.
  • Not Runnable: Sometimes a thread transitions to the waiting state while the thread waits for another thread to perform a task.In this stage thread can in any stage wait(),sleep() etc.
  • Dead: A thread can be considered dead when its run() method completes. If any thread comes on this state that means it cannot ever run again.

Thread Sleep() vs Yield()

Yield() :yield() method pauses the currently executing thread temporarily for giving a chance to the remaining waiting threads of the same priority to execute. If there is no waiting thread or all the waiting threads have a lower priority then the same thread will continue its execution. The yielded thread when it will get the chance for execution is decided by the thread scheduler whose behavior is vendor dependent.

Sleep() :Thread.sleep causes the current thread to suspend execution for a specified period. This is an efficient means of making processor time available to the other threads of an application or other applications that might be running on a computer system. The sleep method can also be used for pacing and waiting for another thread with duties that are understood to have time requirement.
Sleep method in Java has two variants one which takes millisecond as sleeping time while other which takes both mill and nano second for sleeping duration.

  • sleep(long millis)
  • sleep(long millis,int nanos)

Thread Join() method

Join method in Java has two variants one which has no argument and other which takes time in millisecond.

  • join()
  • join(long millisec)
A call to any of these two methods invoked on a thread will wait and not return until either the thread has completed or it is timed out after the specified time, respectively.

Thread isAlive() method

final boolean isAlive() method can be used to find out if a thread is alive or dead. A thread is alive if it has been started but not yet terminated, that is, it is not in the Dead state. The isAlive( ) method returns true if the thread upon which it is called is still running. It returns false otherwise