What are the differences between wait() and sleep() in Java?
Java is a popular server-end programming language for back-end development. A major benefit of using Java is its ability to manage multiple threads. The wait() and sleep() methods are used for managing threads. However, they serve different purposes and have distinct behaviors.
The wait() method
The accessibility of the
wait()method applies to all Java objects as it’s defined within theObjectclass.The primary function of the
wait()method is inter-thread communication and synchronization, especially when managing threads that share resources.Upon invoking the
wait()method, the thread releases the intrinsic lock (also known as the monitor) associated with that object. This allows alternative threads to obtain the lock and enter synchronized code blocks.The thread that makes the call enters a state of “waiting” and stays inactive till another thread invokes
notify()(ornotifyAll()) on the same object or a predetermined period of time elapses.wait()is commonly employed alongside the synchronized keyword to guarantee effective synchronization and coordination among threads.
The sleep() method
The primary role of the
sleep()method is to introduce a delay or pause the thread execution. Thesleep()method is a function of theThreadclass.In the duration that the calling thread sleeps, concurrent threads execute their tasks, and the calling thread will resume execution once the sleep duration has passed.
wait() vs. sleep()
The
wait()method is primarily utilized in synchronization and inter-thread communication. In comparison,sleep()is commonly invoked to pause the execution of threads.Another major difference is that
wait()employs intrinsic locks and needs communication through thenotify()ornotifyAll()functions to release the locks, whereassleep()does not depend on any locks to be released.The invocation of
wait()occurs on an object and is utilized in conjunction with synchronization constructs, whereassleep()is directly called on a thread.The
wait()method is invoked on an object, and its purpose is in conjunction with synchronization constructs. On the other hand,sleep()is directly called on a thread.
Code example
The working example below demonstrates these differences:
public class main {public static void main(String[] args) throws InterruptedException {Object intrinsic_lock = new Object();Thread wait_example = new Thread(() -> { //Thread to demonstrate wait() executionsynchronized (intrinsic_lock) {try {System.out.println("First thread is waiting...");intrinsic_lock.wait(); // Invoking wait() on the intrinsic lock to pause thread executionSystem.out.println("First thread has resumed execution.");} catch (InterruptedException e) {e.printStackTrace();}}});Thread sleep_example = new Thread(() -> { // Thread to demonstrate sleep() executiontry {Thread.sleep(3000); // Introducing a 3 second delay in thread executionsynchronized (intrinsic_lock) {System.out.println("Second thread signals threads to wait.");intrinsic_lock.notify(); // Releasing the intrinsic lock to resume execution of the wait_example thread}} catch (InterruptedException e) {e.printStackTrace();}});// starting thread executionwait_example.start();sleep_example.start();// combining the threads together for sychronized executionwait_example.join();sleep_example.join();}}
Code explanation
Line 9: The
wait_examplethread invokeswait()to pause thread execution and then waits for a notification from thesleep_examplethread to resume.Lines 19–22: The
sleep_examplethread invokes sleep to add a delay in execution before notifying thewait_examplethread to resume execution.
Free Resources