Thread Scheduling

⭐ Interview Importance: MEDIUM
⏱️ Revision Time: 5 min

TL;DR

  • Thread Scheduling is the mechanism the Operating System uses to decide which threads should run on the CPU at any given exact millisecond.
  • Java does not strictly control thread scheduling; it delegates it entirely to the host Operating System.
  • The OS generally uses Preemptive Scheduling based on Thread Priorities.

Concept

If your computer has 8 CPU cores, it can strictly only do 8 things simultaneously. Yet, a typical OS runs thousands of threads.

The Thread Scheduler handles this by giving a thread a small slice of time (Time Slicing). Once the time is up, it pauses the thread (Context Switch) and gives the CPU core to the next thread.

In Java, you can influence the scheduler using Priorities. Thread priorities range from 1 (MIN_PRIORITY) to 10 (MAX_PRIORITY). However, priority is only a hint to the OS, not a guarantee.

Examples

public class ThreadSchedulingExample {
    public static void main(String[] args) {
        
        Thread lowPriority = new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                System.out.println("Low priority working...");
            }
        });
        
        Thread highPriority = new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                System.out.println("High priority working!!!");
            }
        });
        
        // Give hints to the OS scheduler
        lowPriority.setPriority(Thread.MIN_PRIORITY); // 1
        highPriority.setPriority(Thread.MAX_PRIORITY); // 10
        
        // There is NO GUARANTEE the high priority thread will finish first,
        // but it is statically more likely to get more CPU time slices.
        lowPriority.start();
        highPriority.start();
    }
}

Interview Questions

Q: What is Thread.yield()?
A: Thread.yield() is a static method that tells the thread scheduler: “I am doing okay, but if there is another thread of equal or higher priority waiting for the CPU, I am willing to pause my execution and give up my remaining time slice.” Like priorities, it is only a hint. The OS is free to completely ignore the yield request and keep executing the thread.

Q: Why is relying on Thread Priorities dangerous in Java?
A: Because Java relies on the underlying OS scheduler, thread priorities map differently on Windows, Linux, and MacOS. A priority of 10 might aggressively block other threads on one OS, but behave perfectly normally on another. Relying on thread priorities for application correctness or logic flow is guaranteed to result in platform-dependent, unpredictable bugs. Priorities should only be used for minor optimization tweaks.