Monday 6 February 2012

Thread Scheduling in Java

We know that threads can run concurrently.Is it really running concurrently ?
Most computer configurations have a single CPU, so threads actually run one at a time in such a way as to provide an illusion of concurrency.Execution of multiple threads on a single CPU, in some order, is called scheduling.Features are

  • The JVM schedules using a preemptive , priority based scheduling algorithm.
  • All Java threads have a priority and the thread with the highest priority is scheduled to run by the JVM.
  • In case two threads have the same priority a time-slicing mechanism is followed.
  • The Java runtime does not implement (and therefore does not guarantee) time-slicing. However, some systems on which you can run Java do support time-slicing. Your Java programs should not rely time-slicing as it may produce different results on different systems. 


Thread Priority
  • A thread inherits its priority from the thread that created it.
  • A thread's priority can be modified at any time after its creation using the  setPriority() method. 
  • Thread priorities are integers ranging between Thread.MIN_PRIORITY(1) and Thread.MAX_PRIORITY(10). 
  • Main thread's default priority is Thread.NORM_PRIORITY (5).
  • The higher the integer, the higher the priority.

preemptive, priority based scheduling

At any given time, when multiple threads are ready to be executed, the runtime system chooses the "Runnable" thread with the highest priority for execution.

A lower priority thread can start execution only after the higher priority thread stops,yields or becomes "Not Runnable" for some reason.


The Java runtime system's thread scheduling algorithm is also preemptive.
If a thread with a higher priority than the currently executing thread needs to execute, the higher priority thread is immediately scheduled by preempting the lower priority thread.

Round Robin Scheduling


  • In the round robin scheduling, threads are dispatched in a FIFO manner but are given a limited amount of CPU time called a time-slice or a quantum.

  • If a threads does not complete before its CPU-time expires, the CPU is preempted and given to the next threads waiting in a queue. The preempted threads is then placed at the back of the ready list.

  • Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective in time-sharing environments in which the system needs to guarantee reasonable response times for interactive users.


Time-slicing comes into play when there are multiple "Runnable" threads of equal priority and those threads are the highest priority threads competing for the CPU.

If two threads of the same priority are waiting for the CPU, the scheduler chooses one of them to run in a round-robin fashion. The chosen thread will run until one of the following conditions is true:
  • a higher priority thread becomes "Runnable"
  • it yields, or its run() method exits
  • on systems that support time-slicing, its time allotment has expired
Then the second thread is given a chance to run, and so on, until the interpreter exits.

The Java runtime does not implement (and therefore does not guarantee) time-slicing. However, some systems on which you can run Java do support time-slicing. Your Java programs should not rely time-slicing as it may produce different results on different systems.

1 comment: