Java – Thread Scheduler

In this article, we will discuss Thread scheduler and the role it plays in scheduling in detail

1. Thread Scheduler :

  • It is the part of JVM which is responsible to schedule Threads
  • In other words, Thread Scheduler decides which –
    • thread should get the opportunity to run/execute in CPU/processor
    • thread should wait in the queue to get its opportunity
  • If there are multiple threads waiting for their opportunity to run/execute then Thread Scheduler is the one which allocates CPU/processor for some thread making other threads to wait in the queue depending on the below factors
    1. Thread Priority
    2. Arrival Time

1.1 Thread Priority :

  • Every Thread in Java has some priority assigned to it which is inherited from its parent Thread by default
  • Sometimes, programmer can also assign Thread priority based on the business requirement/purpose
  • The valid range of Thread Priority is 1 to 10 where –
    • 1 being lowest priority
    • 10 being highest priority
  • Thread class defines following constants to represent standard Thread priorities
    1. Thread.MIN_PRIORITY -> 1
    2. Thread.NORM_PRIORITY -> 5
    3. Thread.MAX_PRIORITY -> 10
  • So, Thread Scheduler allocates CPU/processor to Thread with higher priority

Q) What if there are multiple threads of same Thread priority ?

  • If there are multiple threads with same Thread priorities waiting in the Runnable state then Thread Scheduler uses different algorithms to allocate CPU/processor to Thread

1.2 Arrival Time :

  • If there are multiple threads of same priorities waiting for their opportunity to run/execute then Thread Scheduler picks the thread that arrived first in the Runnable state making other threads wait in the queue
  • But this is one of the algorithm followed by the Thread Scheduler known as First-come First-serve or in short FCFS
  • There are other algorithms followed by Thread Scheduler to prevent starvation

2. Thread Scheduler Algorithms :

Thread Scheduler uses below algorithms to allocate processor to Threads

  1. First-come First-serve (FCFS)
  2. Time Slicing (TS)
  3. Pre-emptive Priority (PEP)

2.1 First-come First-serve (FCFS) :

  • In FCFS algorithm, Thread Scheduler picks Thread which arrived first in the Runnable state
  • Assume there are 3 different threads which arrived in the Runnable state in the following order
    • first thread t1
    • second thread t2
    • third thread t3
  • Then, Thread Scheduler allocate processor to thread t1 first and then after completion of thread t1 it allocates thread t2 and likewise thread t3
  • But the issue with this algorithm is that, FCFS is non pre-emptive which means that until thread t1 completes Thread Scheduler won’t allocate processor to threads t2 & t3 and this leads to starvation

2.2 Time Slicing (TS) :

  • In TS algorithm, Thread Scheduler slices timeframe into equal partition for each & every threads in the Runnable state
  • It works in the roundrobin fashion where Thread Scheduler allocates CPU/processor of equal timeslice to every threads in the Runnable state preventing Starvation
  • Assume there are 3 different threads in the Runnable state and time-slice of 2 seconds
    • thread t1
    • thread t2
    • thread t3
  • Then, Thread Scheduler allocates CPU/processor to thread t1 and then to thread t2 irrespective of whether thread t1 is completed or not and like-wise for thread t3
  • Although, TS algorithm prevents Starvation problem but it faces challenges with the responsetime

2.3 Pre-emptive Priority (PEP) :

  • In PEP algorithm, Thread Scheduler allocates CPU/processor to thread with highest priority but this will lead to starvation therefore time-slices also provided so that highest priority thread has to release or give up CPU/processor to other low priority waiting threads in the Runnable state
  • Assume there are 3 different threads in the Runnable state and time-slice of 2 seconds
    • thread t1 (priority P1)
    • thread t2 (priority P2)
    • thread t3 (priority P3)
  • Then, at first Thread Scheduler allocates CPU/processor to thread t1 with highest priority P1 but after the time-slice of 2 seconds it has to give up processor to other low priority waiting threads like P2 & P3 even though thread t1 with highest priority hasn’t completed its job
  • In all 3 algorithms discussed so far, this give fair chance to every waiting threads in the Runnable state as –
    • at first Thread Scheduler allocates CPU/processor to thread with highest priority
    • after time-slice of 2 seconds, thread with highest priority has to give up processor for the remaining low priority waiting threads irrespective of whether job is completed or not for the highest priority thread

Related Articles :

  • Java – Introduction to Threads
  • Java – Different ways to create/spawn a Thread
  • Java – Thread Scheduler
  • Java – Important methods and constructors of a Thread class
  • Java – Difference between start() and run() methods of Thread class
  • Java – Importance of start() method in Thread class
  • Java – Overloading run() method of Thread class
  • Java – Effects of not overriding run() method of Thread class
  • Java – Setting and getting name of a Thread
  • Java – Thread priorities

References :

Happy Coding !!
Happy Learning !!

Java – Important methods and constructors of a Thread class
Java – Thread class v/s Runnable interface