Last Updated on August 22, 2020 by Scientech Easy
Thread scheduler in Java is the component of JVM that determines the execution order of multiple threads on a single processor (CPU).
It decides the order in which threads should run. This process is called thread scheduling in Java.
When a system with a single processor executes a program having multiple threads, CPU executes only a single thread at a particular time.
Other threads in the program wait in Runnable state for getting the chance of execution on CPU because at a time only one thread can get the chance to access the CPU.
The thread scheduler selects a thread for execution from runnable state. But there is no guarantee that which thread from runnable pool will be selected next to run by the thread scheduler.
Java runtime system mainly uses one of the following two strategies:
1. Preemptive scheduling
2. Time-sliced scheduling
This scheduling is based on priority. Therefore, this scheduling is known as priority-based scheduling. In the priority-based scheduling algorithm, Thread scheduler uses the priority to decide which thread should be run.
If a thread with a higher priority exists in Runnable state (ready state), it will be scheduled to run immediately. In case more than two threads have the same priority then CPU allocates time slots for thread execution on the basis of first-come, first-serve manner.
The process of allocating time to threads is known as time slicing in Java. Time-slicing is based on non-priority scheduling. Under this scheduling, every running thread is executed for a fixed time period.
A currently running thread goes to the Runnable state when its time slice is elapsed and another thread gets time slots by CPU for execution.
With time-slicing, threads having lower priorities or higher priorities gets the same time slots for execution.
Hope that this tutorial has covered the basic points of time slicing and thread scheduler in Java. I hope that you will have understood this topic. In the next tutorial, we will learn thread priority.
Thanks for reading!!!
Next ⇒ Thread Priority in Java⇐ PrevNext ⇒