Thread scheduler


Thread scheduler

Multiple threads’ execution on a single CPU in a given order is termed as scheduling. Thread scheduler example can be seen below:

Working of the above example:

  • They are 4 threads created with different priorities. The decision of which thread should go into the processor is decided by the JVM.
  • The higher priority thread is given by the scheduler to the cpu for execution. Meanwhile, if some other thread with a higher priority arrives, then the first thread is pre-empted from the processor and the higher priority thread further completes its execution. This is known as pre-emptive scheduling.
  • If two threads have the same priority then First Come First Serve scheduling is used.

Three main algorithms of scheduling are there, which are as follows:

  • Pre-emptive priority scheduling: It schedules the threads based on their priority concerning the Runnable threads. When multiple threads are ready for execution, the runtime system chooses to execute the threads with higher priority. 
  • First come, first serve scheduling: The java VM gives the time-slicing to the threads based on their arrival time and then schedules their jobs accordingly. The job coming first in the ready queue will be the first one to get the CPU.
  • Time slicing algorithm: This is a preemptive FCFS algorithm, and here each thread executes cyclically. The system specifically defines a time slice, called as a quantum. Every thread in the ready queue gets a quantum time assigned to it, and that is when it can access the CPU in a single go.