The element in Java responsible for determining the order of thread execution and waiting is known as the thread scheduler in Java. A thread in Java is selected by the thread scheduler when it is in the runnable state. When multiple threads are in the runnable state, the thread scheduler makes the decision to select one thread for execution while leaving the others waiting. Various criteria are employed to determine the order of thread execution, with priority and time of arrival being the two main factors considered in scheduling a thread.
Thread priority ranges from 1 to 10, determining the likelihood of a thread being selected by the thread scheduler. Higher priority threads have an increased chance of being selected.
Arrival Order Importance: In a scenario where two threads with identical priorities transition to the runnable state simultaneously, priority is not the determining factor in selecting one over the other. In this situation, the scheduler gives precedence to the thread that was first to arrive, considering its arrival time as the decisive factor.
Thread Scheduler Algorithms
According to the factors outlined above, the Java thread scheduler implements the scheduling algorithm.
First Come First Serve Scheduling:
In this particular scheduling method, the scheduler selects the threads that are the earliest to arrive in the runnable queue. Refer to the table below for further details:
| Threads | Time of Arrival |
|---|---|
t1 |
0 |
t2 |
1 |
t3 |
2 |
t4 |
3 |
In the above table, we can see that Thread t1 has arrived first, then Thread t2, then t3, and at last t4, and the order in which the threads will be processed is according to the time of arrival of threads.
Therefore, Thread t1 will be executed initially, while Thread t4 will be the final one to be processed.
Time-slicing scheduling:
Typically, the First Come First Serve algorithm operates in a non-preemptive manner, which can result in indefinite blocking, commonly referred to as starvation. To prevent this issue, time slices are allocated to threads. This ensures that after a certain period, the currently executing thread must relinquish control of the CPU. Consequently, this allows other threads in the queue the opportunity to execute their tasks.
In the diagram shown above, every thread is allocated a time slice of 2 seconds. Consequently, once 2 seconds elapse, the initial thread vacates the CPU, allowing Thread2 to take over. This pattern continues for the remaining threads in a similar manner.
Preemptive-Priority Scheduling:
The term of the scheduling algorithm indicates that it is associated with the prioritization of threads.
In a scenario where numerous threads are in a state ready to run, the scheduler selects the thread with the top priority. This preemptive algorithm ensures that time slices are allocated to threads to prevent any from being starved of resources. Consequently, even if the highest priority thread has not finished its task after a period, it must relinquish the CPU due to preemption.
Working of the Java Thread Scheduler
Let's explore how the Java thread scheduler operates. Imagine a scenario where there are five threads with varying arrival times and priorities. In this situation, the thread scheduler is tasked with determining the order in which each thread will be granted access to the CPU.
The thread scheduler prioritizes the selection of the thread with the highest priority to commence the execution of a task. In a scenario where a thread is already in a state ready for execution and another thread, with a higher priority, enters this ready state, the current thread is preempted from the processor. Subsequently, the thread with the higher priority takes over the CPU time.
In a scenario where two threads, namely Thread 2 and Thread 3, share identical priorities and arrival times, the scheduling process will be determined using the First-Come-First-Serve (FCFS) algorithm. Consequently, the thread that reaches the system first will be granted the chance to run initially.