Tuesday, December 10, 2013

Thread Scheduling

Execution of multiple threads on a single CPU in some order is called Thread Scheduling. The Java Runtime Environment supports a very simple, deterministic algorithm called fixed-priority scheduling. This algorithm schedules threads on the basis of their priority relative to other Runnable threads.

When a thread is created, it inherits the priority from the thread that creates it. In addition, by using the setPriority method, you can modify a thread's priority at any time after its creation.

Thread priorities ranges between MIN_PRIORITY and MAX_PRIORITY.

Following are the main features of Thread Scheduling:
  • The JVM schedules using a pre-emptive, priority based algorithm.
  • All 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, FIFO ordering is followed.
  • A different thread is invoked to run in case one of the following events occur:
    • The currently running thread exits the Running state. i.e., either blocks or terminates.
    • A thread with a higher priority than the thread currently running enters the Runnable state. The lower priority thread is preempted and the higher priority thread is scheduled to run.
  • Time slicing is dependent on the algorithm implementation.
  • A thread can voluntarily give up its right to execute at any time by calling the yield() method. This process is called Cooperative Multitasking. Threads can yield the CPU only to other threads of the same priority. Attempts to yield to a lower-priority thread are ignored.

No comments:

Post a Comment