Mastering Java Concurrency: Essential Interview Questions on Multithreading, Concurrency.

1. What is the difference between sleep() and yield() methods in Java threads?

The sleep() method causes the currently executing thread to pause its execution for a specified duration, allowing other threads to execute. While sleeping, a thread does not release any resources or locks it holds. In contrast, yield() suggests to the thread scheduler that the current thread is willing to yield its current use of the processor. The scheduler is free to ignore this suggestion, and it does not cause the thread to become blocked or inactive; it simply moves to the runnable state, allowing other threads of the same priority to get a chance to execute.

To illustrate the difference between the sleep() and yield() methods in Java threads, let’s create a simple example that demonstrates how each method affects thread execution.

Java Example to Illustrate sleep() vs yield()

This example consists of two threads: one uses sleep(), and the other uses yield(). Both threads attempt to print numbers to the console, allowing us to observe the behavior of sleep() and yield() in terms of thread scheduling and execution control.

javaCopy code

public class SleepVsYieldExample {
    public static void main(String[] args) {
        // Thread using sleep()
        Thread sleepThread = new Thread(() ->{
            for(int i=0; i<=5; i++){
                System.out.println("Sleep Thread: "+i);
                try {
                }catch (InterruptedException exception){
        //Thread using yield()
        Thread yieldThread = new Thread(() -> {
            for(int i=1; i<=5; i++){
                System.out.println("Yield Thread :" + i);

Expected Behavior:

  • Sleep Thread: This thread will print a number, then pause for 2 seconds due to the call to Thread.sleep(2000). This causes the thread to suspend its execution for approximately 2 seconds, allowing other threads to run during this time.
  • Yield Thread: This thread will print a number, then call Thread.yield(), suggesting to the thread scheduler that it is willing to yield its current use of the CPU. The scheduler may then decide to pause the execution of this thread and switch to another thread. However, the effect of yield() is subtle and highly dependent on the underlying operating system’s thread scheduler; it does not guarantee that the thread will stop executing immediately.

Key Differences Illustrated by the Example:

  • Control Over Execution: sleep() allows precise control over how long a thread pauses, while yield() leaves the decision of whether to pause (and for how long) up to the scheduler.
  • Intended Use: sleep() is typically used to pause execution for a specific period, while yield() is used to improve the efficiency of thread scheduling by indicating that a thread does not need to use its entire time slice.

This example demonstrates how sleep() and yield() can be used to manage thread execution, with sleep() providing a way to pause for a specific time and yield() offering a way to suggest to the scheduler that other threads can be run.



About Author

As Neelabh Singh, I am a Senior Software Engineer with 6.6 years of experience, specializing in Java technologies, Microservices, AWS, Algorithms, and Data Structures. I am also a technology blogger and an active participant in several online coding communities.

You may also like

Blog Design Pattern

Understanding the Builder Design Pattern in Java | Creational Design Patterns | CodeTechSummit

Overview The Builder design pattern is a creational pattern used to construct a complex object step by step. It separates
Blog Tech Toolkit

Base64 Decode

Base64 encoding is a technique used to encode binary data into ASCII characters, making it easier to transmit data over