Thread Scheduler in Java

Thread Scheduler

The thread scheduler is one of the part of JVM (mostly JVM takes help of native threads on the underlying OS) and that decides which thread should run at any point of time, blocking of thread like waiting, sleeping, yielding and also kills a thread to reach dead state (it may be premature death or natural death).

Thread scheduler decides which threads are eligible and finally that will actually run. When we say threads are eligible to run that means the thread is in the runnable state. All the threads comes to runnable after started. Any thread from the runnable state can be chosen by the thread scheduler to run and reach in running thread(in case of single processor system). If a thread is not currently in runnable state, then it cannot be chosen by the scheduler to run. There is little guarantee to reach running state if there are large number of threads on the runnable state and also the order of the threads to run is not guaranteed.

Below methods can help us to influence thread scheduling are as follows:

These are Thread class methods –

public static void sleep(long millis) throws InterruptedException
public static void yield()
public final void join() throws InterruptedException
public final void setPriority(int newPriority)

Every class in Java inherits the following three thread-related methods:

These are all Object class methods –

public final void wait() throws InterruptedException
public final void notify()
public final void notifyAll()

Thread scheduler uses algorithms to schedule thread, some of them are below:

Priority based(round-robin): a thread of higher priority will run first then a thread of lower priority. In case two threads have the same priority a FIFO ordering is followed.

Time Slice: number of allocated timeslices of CPU, which essentially determines the amount of CPU time a thread is allotted before it is forced to yield the CPU to another thread of the same or lower priority

Gopal Das
Follow me

Gopal Das

Founder at GopalDas.Org
He is a technology evangelist, Salesforce trainer, blogger, and working as a Salesforce Technical Lead. After working in Java based project implementation, he jumped to the Salesforce system on a whim and never looked back. He fell in love with Salesforce’s flexibility, scalability, and power. He expanded his knowledge of the platform and became a Certified App Builder, Administrator, Platform Developer I, SalesCloud Consultant while leading the Salesforce implementation and technology needs. He has worked in a wide variety of applications/services like desktop, web and mobile applications.
Gopal Das
Follow me

Leave a Reply

Your email address will not be published. Required fields are marked *