top of page
Search

Threads in JAVA -- Part II

Writer's picture: Titash RoyTitash Roy

Introduction In the last post, we have had an introduction to threads in JAVA and discussed how threads are created. In this post, we will learn about the thread scheduler and thread priority.


Thread Scheduler A thread scheduler in java is the part of the JVM that decides which thread should run and which should wait. It always chooses a thread to run only if it is in the RUNNABLE state. In case of multiple thread being in RUNNABLE state there is no guarantee which would be executed first. In case of multiple runnable threads, the thread scheduler decides n which to run first based on few factors:

  • Priority: While creating the thread, we can set the priority of the thread based from 1-10. 10 being highest and 1 being least. If multiple threads have same priority, scheduler needs to decide which to run and how to set the frequency of the run.

  • Arrival time: The thread scheduler also depends on the arrival time of the thread. If two or more thread has same priority then thread scheduler checks the arrival time of threads.


Priority of Thread Each thread has a priority. It is represented by a number between 1 and 10. In most cases, thread scheduler schedules the threads according to their priority. But it is not guaranteed because it depends on JVM specification that which scheduling it chooses. The scheduling mechanisms are discussed below. There are 3 constants defined in the thread class:

  • public static int MIN_PRIORITY

  • public static int NORM_PRIORITY

  • public static int MAX_PRIORITY

Default priority of a thread is 5 (NORM_PRIORITY). The value of MIN_PRIORITY is 1 and the value of MAX_PRIORITY is 10.


What is time slice? In a multi-threaded environment, where multiple threads run concurrently, the CPU allocates the small amount of time to each thread for the execution. This is known as time slice. The thread scheduler checks each thread and takes the decision which thread will get CPU time first based on the above two factors.


Scheduling System used by Thread Schedulers Thread schedulers use certain mechanisms to choose which thread to run. Below are the mechanisms.

  1. Preemptive-priority scheduling: This algorithm is based on the priority of the thread. If multiple threads are in the RUNNABLE state, the thread scheduler chooses the threads that have the highest priority and will ignore the other thread. The selected thread runs until any of the following situation does not occur.

    1. A higher priority thread becomes runnable.

    2. When the current thread goes for stop the execution. We can use yield(), sleep(), join() method to give the chance to executes the other threads.

    3. If the selected thread has completed its time slice.

  2. First-come First-serve Scheduling: According to this algorithm, the scheduler assigns the CPU time to the thread which comes first. The thread scheduler checks the arrival time of the thread, give the time slice for its execution. Suppose the CPU is already executing multiple threads, meanwhile some more thread comes in the ready queue. The thread scheduler will check the arrival time of all the threads and select the thread for execution which came first.

  3. Time slicing scheduling: This algorithm based on First Comes First Serve and time slice. The thread scheduler assigns time slice to each thread. The time slice is defined in the system and every thread gets executed cyclically. Suppose there are multiple threads present in the ready queue, then the thread scheduler will assign CPU to each thread for a period. If the execution of the thread is completed within a time slice, then the thread scheduler terminates the execution and switch to another thread.


How the scheduler works Based on the above scheduling examples, let us have a look at this example.

  1. Suppose, there are five threads each having different priorities and different arrival times.

  2. The thread scheduler will decide which thread to execute first.

  3. Thread scheduler will select the thread, that has the highest priority and starts the execution of the thread. Meanwhile, if any other thread comes with the highest priority, the then-current thread will be pre-empted from the processor, and the new thread that has the highest priority will get CPU time.

  4. If two threads come with the same priorities (Thread2 and Thread3), then the thread scheduler uses the FCFS scheduling algorithm. The thread scheduler assigns the processor that had the first-come thread.


Summary In this post we have discussed about the thread scheduler. In later posts, we will deep dive into more multi-threading topics.

15 views0 comments

Recent Posts

See All

Comments


bottom of page