Thread Scheduling in Java

In Java you can use methods to help a thread get into the runnable state, but you cannot directly put the thread into the running state. A thread in the runnable state is eventually put into the running state by the scheduler. The scheduler follows some kind of algorithm to determine which thread should go into the running state. 

There are two main categories of scheduling algorithms:

Preemptive scheduling: 

In this type of scheduling a thread is put into the running state according to the priority. While a thread is running, it can be moved out of the running state only by one of the following conditions:
  • It calls one of the blocking I/O methods.
  • A higher-priority thread gets into the runnable state.

Time sharing: 

In this kind of scheduling algorithm, a thread is allowed to execute only for a limited amount of time. After the time expires, it’s put back into the runnable state, and another thread seeking its turn is put into the running state. Time sharing provides protection against a higher-priority thread using the whole CPU time and keeping all other threads from running. 

Remember that the implementation of the scheduling algorithm in Java is platform dependent. Scheduling is performed to share a resource (the CPU) among multiple threads. Other resources in an application, such as a file, are shared properly by synchronizing the access, as you have already seen. However, sharing the same resource makes threads dependent upon each other, and that dependency may give rise to a deadlock.

If you are curious about deadlock and want to know about it, You may find detail about deadlock here. What is deadlock?


Reference(s): SCJP Exam for J2SE 5

Newer Post Older Post

Leave a Reply

naeemgik. Powered by Blogger.