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.
wait()
methodThe accessibility of the wait()
method applies to all Java objects as it’s defined within the Object
class.
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()
(or notifyAll()
) 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.
sleep()
methodThe primary role of the sleep()
method is to introduce a delay or pause the thread execution. The sleep()
method is a function of the Thread
class.
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 the notify()
or notifyAll()
functions to release the locks, whereas sleep()
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, whereas sleep()
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.
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();}}
Line 9: The wait_example
thread invokes wait()
to pause thread execution and then waits for a notification from the sleep_example
thread to resume.
Lines 19–22: The sleep_example
thread invokes sleep to add a delay in execution before notifying the wait_example
thread to resume execution.
Free Resources