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 {
Thread.sleep(2000);
}catch (InterruptedException exception){
exception.printStackTrace();
}
}
});
//Thread using yield()
Thread yieldThread = new Thread(() -> {
for(int i=1; i<=5; i++){
System.out.println("Yield Thread :" + i);
Thread.yield();
}
});
sleepThread.start();
yieldThread.start();
}
}
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 ofyield()
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, whileyield()
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, whileyield()
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.