What is difference between sleep method and yield method of multi threading?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In the world of multithreading in Java, the `sleep()` and `yield()` methods are two important tools from the `Thread` class that developers can use to manipulate thread execution. Though they may seem similar at first glance, they serve quite different purposes. Understanding these differences is crucial for effectively managing threads in concurrent programming.
The `sleep()` Method
The `sleep()` method is used to halt the execution of the current thread for a specified period of time, allowing other threads to execute. It is a static method of the `Thread` class and is commonly utilized for simulating delayed system operations or adding pause points in an execution sequence.
Characteristics of `sleep()`:
- Timing: The thread pauses for a defined duration, specified in milliseconds, and optionally in nanoseconds.
- Precision: The actual duration that the thread sleeps might not be precisely the specified time due to scheduling by the operating system.
- Interruption’s Role: The `sleep()` method can throw an `InterruptedException`, which occurs if another thread interrupts the sleeping thread. Handling this exception is necessary.
- State Change: On calling `sleep()`, the thread transitions out of the CPU's running state and moves into a waiting state.
Example:
- Purpose: It gives a chance for other threads of equal priority to execute. It is primarily used for debugging and testing purposes.
- Priority Influence: Unlike `sleep()`, `yield()` does not take any time parameter or unit, and it depends largely on the thread scheduler’s implementation for influence.
- State Change: Invoking `yield()` simply moves the thread back to the runnable state from the running state, allowing the scheduler to decide the next action.
- No Guarantee of Yield: There is no assurance that the `yield()` call will have any effect. It might be the case that the thread quickly regains control after yielding.
- When to Use: Use `sleep()` when timed pauses are required in your application. It is particularly helpful in scenarios where you want to simulate delay for synchronization or pacing. For `yield()`, its usage can help in reducing resource starvation amongst similar priority threads, but its effects are unpredictable due to JVM and OS scheduler differences.
- Interruption Behavior: The `sleep()` method provides a direct interruption mechanism by implying `InterruptedException`, which makes it extensively useful for cooperative thread management. `yield()`, on the other hand, provides no such mechanism and is therefore less reliable for thread communication purposes.
Related reading
- What is exactly for Custom Coroutine Scope?
- What is lock-free multithreaded programming?
- What is Locked ownable synchronizers in thread dump?
- What is mutex and semaphore in Java ? What is the main difference?
- What is Dispatcher Servlet in Spring?
- What is Double Brace initialization in Java?
- What is new in multithreading in Delphi XE?
- What is recommended way to perform async tasks in WPF?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.