Table of contents
1.
Introduction
2.
Thread Scheduler
3.
Preemptive Scheduling
4.
Time Slicing Scheduling
5.
First Come, First Serve based Scheduling
6.
Working of a Thread Scheduler
7.
Methods of Scheduler
8.
Example
8.1.
Code 1
9.
FAQs
10.
Key Takeaways
Last Updated: Mar 27, 2024

Thread scheduler

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The thread scheduler is also an essential concept of Java programming that decides which thread to execute and which thread to wait for. In Java, the thread is only chosen by the thread scheduler.

Must Read, Multithreading in java. Also see, Duck Number in Java

Thread Scheduler

A Thread scheduler is one of the essential concepts in multi-threading. As the name suggests, scheduler means it decides which thread to choose for its execution. These choices depend mainly upon two factors which are time-slicing and preemptive scheduling. A Thread scheduler chooses one of those threads in the Runnable state.

Preemptive Scheduling

The thread scheduler works on choosing the higher priority threads for its execution. When the processor executes some high-priority thread, and if another thread comes with high priority, it transfers its thread from Runnable to Waiting state and allows the high-priority thread to manage. This process is known as preemptive scheduling.

Time Slicing Scheduling

It is the process in which the Java Thread scheduler assigns some fixed time slot for the thread’s execution. The thread executes for a particular time then re-enters in the active pool. Time slicing is the specific time allotted to the processor to execute all the threads.

First Come, First Serve based Scheduling

 This scheduling is similar to the queue where the scheduler chooses the thread that comes first to the ready pool for execution. In simple words, the thread scheduler prioritizes the threads with more waiting time or arrives first in the prepared collection. This scheduling comes in handy when multiple threads of the same priority occur.

Working of a Thread Scheduler

In Java, a thread scheduler works on different mechanisms like preemptive scheduling time slicing and First come, first serve based scheduling. A process might contain several threads where every function has a priority and a waiting time solely based on these two factors, picking up the thread for its execution and sending it to the processor.

Let us take an example to explain its works.

  • Initially, the processor has three threads with a priority of 8,3, and 5. They are here based on the preference the thread scheduler picks the thread 1 having high priority.
  • Then after it will pick thread three and send it to the processor for execution.
  • Suppose another thread 4 comes with a priority of 6 in the waiting pool. Now the scheduler will pause the ongoing process and preempts thread 3. It will then pick up thread four since it has high priority and transfer its execution to the processor.
  • After executing these threads, another thread 5 comes with the priority of 3 having the same as thread 2. Based on the FCFS concept, the processor will pick thread two since it has more waiting time and has arrived first than thread 5.
     

Methods of Scheduler

Also see, Hashcode Method in Java

Example

Code 1

class X extends Thread {
    int a = 1;

    public void run() {
        System.out.println("Thread X started");
        while (a <= 3) {
            System.out.println("Value: " + a + " in Thread X");
            a++;
        }
        System.out.println("Thread X completed");
    }
}

class Y extends Thread {
    int b = 1;

    public void run() {
        System.out.println("Thread Y started");
        while (b <= 3) {
            System.out.println("Value: " + b + " in Thread Y");
            b++;
        }
        System.out.println("Thread Y completed");
    }
}

class Z extends Thread {
    int c = 1;

    public void run() {
        System.out.println("Thread Z started");
        while (c <= 3) {
            System.out.println("Value: " + c + " in Thread Z");
            c++;
        }
        System.out.println("Thread Z completed");
    }
}

public class thread_test {
    public static void main(String[] args) {
        System.out.println("Main thread");
        X x = new X();
        Y y = new Y();
        Z z = new Z();

        Thread t = Thread.currentThread();
        System.out.println(t.getName());

        t.setPriority(Thread.MAX_PRIORITY);
        y.setPriority(Thread.MIN_PRIORITY);
        z.setPriority(Thread.NORM_PRIORITY);

        x.start();
        y.start();
        z.start();
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

Main thread
main
Thread X started
Thread Z started
Thread Y started
Value: 1 in Thread Z
Value: 1 in Thread X
Value: 2 in Thread Z
Value: 1 in Thread Y
Value: 3 in Thread Z
Value: 2 in Thread X
Thread Z completed
Value: 2 in Thread Y
Value: 3 in Thread Y
Thread Y completed
Value: 3 in Thread X
Thread X completed
You can also try this code with Online Java Compiler
Run Code

In this example, we have taken a simple execution of a thread with three threads. We executed three different threads one by one. We also have created the object for all these threads. Then we have used other in-built methods such as :
MAX_Priority: This method sets the thread’s priority to be the topmost 

MIN_Priority: This method sets the thread’s priority too low.

NORM_PRIORITY: Sets the default priority of the thread. After we have used the start method to begin the thread.

Practice by yourself on java online compiler.

FAQs

  1. How do you schedule a thread in Java?
    A component of Java that helps in deciding that which thread to run or execute and which thread to wait is called thread scheduler in Java. A thread is only chosen by a thread scheduler only if it is in a runnable state. 
     
  2. What is the function of preemptive scheduling in a thread scheduler?
    The critical part of preemptive scheduling is to schedule the threads. Under this scheduling, the thread with the highest priority gets executed first until it enters the waiting or dead states or the new higher priority task comes into existence.

Key Takeaways

This blog covered thread scheduler. We have covered its introduction and what is a thread scheduler. We also talked about different types of scheduling that come under thread scheduling. We also spoke about preemptive scheduling and time slicing scheduling. We also covered the working of the thread scheduler.

Recommended Readings:

If you are curios about operating system and what goes behind working of an OS, you can visit Introduction to Operating Systems.

If you want to learn more about these concepts and practice more questions, you can have a look at our library of blogs curated by experts.

Live masterclass