Thread scheduler in java

Most programmers know about multithreading in java, but few of the programmers know how a thread schedule for the exception? How JVM executes multiple threads executing concurrently? A thread scheduler schedules the thread for execution. So now the question arises that which thread should be executed first? In this post, we will find how does thread scheduler in java schedules the threads.

Here is the table content of the article that will cover this topic.
1. What is the thread scheduler in java?
2. How does thread scheduler work?
3. Preemptive-priority scheduling
4. First come First serve Scheduling(FIFS)
5. Time-slicing scheduling
6. Working with the Thread Scheduler?

What is the thread scheduler in java?

A thread scheduler in java is the part of the JVM that decides which thread should run and which should wait. The thread scheduler always chooses a thread to run only if it is in the RUNNABLE state. But there is no guarantee which thread will be chosen to run if you have multiple threads in the RUNNABLE state.

A thread scheduler has the responsibility to execute the threads. Suppose there are a number of threads, then the thread scheduler will decide which thread should be run first. 

There are a number of factors or criteria that are used to select a thread.

Priority
Whenever we create a thread, it always inherits priority from its parent thread. Each thread has a priority that lies between 1 to 10. The higher priority of the thread means a higher chance to get selected for the execution. But if multiple threads have the same priority then the thread scheduler can select any random thread. We will discuss it in detail.

Arrival time
The thread scheduler also depends on the arrival time of the thread. If two or more thread has the same priority then the thread scheduler checks the arrival time of the threads.

How does the thread scheduler work?

As we know in a multithreading environment, multiple threads can run concurrently. But the CPU allocates a small amount of time(That is known as a time slice) to each thread for the execution. The thread scheduler checks each thread and takes the decision on which thread will get CPU time first. The CPU takes this decision based on several factors which we have discussed above.

The thread scheduler always chooses the thread for execution only if it is already in the RUNNABLE state. Suppose we have three threads in a program, Now let’s see how to thread scheduler is assigning CPU time to each thread.

Thread scheduler in java

Here CPU assigns 3 seconds to each thread and all threads work parallelly. There is some scheduling algorithm is using by thread scheduler to schedule the threads. We will discuss them below.

Preemptive-priority scheduling

As you can see the name of the algorithm already says priority scheduling. This algorithm is based on the priority of the thread. Suppose the multiple threads are in the RUNNABLE state (ready to run), the thread scheduler chooses the threads that have the highest priority. It will ignore the other’s thread and select the higher-priority thread.
The selected thread runs until a certain situation does not occur  That are:
1. A higher priority thread becomes runnable,
2. When the current thread goes for stop the execution. We can use yield(),  sleep(), join() method to give the chance to execute the other threads.
3. If the selected thread has completed its time slice.

If any higher priority thread comes in the RUNNABLE state or the time slice of a given thread has finished. Then the current running thread will be suspended and another thread having higher priority will get a time slice for completing its task in the Running state.  This type of scheduling is called Preemptive-priority scheduling.

Thread scheduler in java

First come First serve Scheduling(FIFS)

According to this algorithm, the thread scheduler assigns the CPU time to the thread which comes first. The thread scheduler checks the arrival time of the thread and gives the time slice for its execution. Suppose the CPU is already executing multiple threads, meanwhile some more thread comes in the ready queue. The thread scheduler will check the arrival time of all the threads and select the thread for execution that came first.

Time-slicing scheduling

This algorithm is based on First Come First Serve and time slice. The thread scheduler assigns a piece of time to each thread which is known as a time slice. The time slice is defined in the system and every thread gets executed cyclically. Suppose there are multiple threads present in the ready queue, then the thread scheduler will assign CPU to each thread for a period. If the execution of the thread is completed within a time slice, then the thread scheduler terminates the execution and switches to another thread.

Working with the Thread Scheduler

Thread scheduler in java

Step 1:  There are five threads having different priorities and different arrival times.

Step 2:  The thread scheduler will decide which thread goes first for the CPU time.

Step 3: The thread scheduler will select the thread, that has the highest priority and starts the execution of the thread. Meanwhile, if any other thread comes with the highest priority, the then-current thread will be pre-empted from the processor, and the new thread that has the highest priority will get CPU time.

Step4: If two threads come with the same priorities (Thread2 and Thread3), then the thread scheduler uses the FCFS scheduling algorithm. The thread scheduler assigns the processor that had the first-come thread.

Leave a Comment