Java has several methods to carry out certain actions. Method is a group of code, which runs only when it is called; and may or may not return a result.
Methods like sleep and wait are used for multithreading. Both of them pauses and sends the thread into waiting, but have major differences in functioning.
Sleep vs Wait
The main difference between sleep() and wait() method is that the sleep() method is used in the program to pause the execution of current thread for a particular time period while the wait() method is used in the program to pause or suspend the current thread until specific methods are invoked.
During the use of sleep method, the current thread does not lose the ownership of the monitor. The sleep is a static method and is a part of class thread. After the wait time gets over, the thread goes back to the original runnable state. It ensures complete utilization of CPU, while waiting.
Whereas the wait() method is used in the Java program to order the current thread to wait, until another thread is not invoked for that object. The thread then continues with the execution, once it obtains the control of the monitor. It is not a static method unlike sleep, and is a part of object class.
Comparison Table Between Sleep and Wait
Parameters of comparison | Sleep | Wait |
Class | The sleep method is a part of thread class | The wait method is a part of object class |
Type of method | Sleep is a static method | Wait is not a static method |
Calling technique | The sleep method can be called from outside the synchronized context | The wait method can be called only from the synchronized context |
Lock release | Sleep method does not release the lock on the object, for the specified timeout, during synchronization | Wait method releases the lock on the object, to have a chance to execute, during synchronization |
Declaration | public static void sleep() | public final void wait() |
What is Sleep?
The thread sleep() is static method in Java program which suspends the current thread and puts it in wait state for a stipulated time period. Once the wait state and time is over, the thread condition is changed to runnable state. And then waits for the CPU for further execution.
The aim of this method is to create delay for few seconds in the program and simultaneously have maximum utilization of the CPU. If the system is busy or overloaded then the wait or pause time is more and otherwise it would be less or equal to the actual time.
The return type of sleep method can be said void, as it does not return any value. Sleep thread does not lose the monitor or lock the thread that it has acquired already. If in any case the sleep is disturbed, the thread would throw Interrupted_Exception. The actual duration for which the thread would sleep usually depends on the schedulers and system timers which are a part of the operating system.
The syntax of sleep method is – public static void sleep(long milliseconds)
public static void sleep(long milliseconds, int nanoseconds)
Here milliseconds and nanoseconds is the time for which the thread would sleep.
What is Wait?
Wait() is a method used for inter-thread communication. Whenever the wait() thread is used, the calling or current thread is paused and suspended, until methods like notify() or notifyAll() are invoked in the system.
notify() method would wake specified thread while notifyAll() is applicable for every thread. When synchronized, the wait method gives up the lock to the resources. There is no return value of the Wait method, hence it can be said that it returns void.
The two exceptions of the Wait method are Interrupted_Exception (when the current thread is interrupted during its sleep) and IllegalMonitorStateException (when the current thread is not the owner of the object on monitor).
The Wait method is a part of the object class. The method is only applicable and can be called over a synchronized block. The wait method sets the suspended or locked screen free, during synchronization. And the Wait method exclusively allows the synchronized multiple threads to access the same object one by one.
The syntax of the Wait method is – public final void wait()
Main Differences Between Sleep and Wait
- The Sleep method causes the thread to sleep for a specified and particular time period usually for specified timeout unless expired or interrupted while the Wait method causes the thread to sleep until methods like notify() or notifyAll() are invoked.
- The Sleep method executes on a thread while the Wait method executes on an object.
- The Sleep method is used for time-synchronization while the Wait method is used for multiple-thread synchronization.
- During Wait thread, synchronized multiple threads can access the same object adjacently while in Sleep thread, synchronized multiple threads have to wait for the sleep of the current thread to get over.
- The Wait method sets the lock or monitor free, while Sleep method does not set the monitor free during sleep or wait of the current thread.
Conclusion
The Sleep and Wait method are native methods of Java. And both are extensively used to make the current ongoing thread go into a state of non-runnable condition. Both of the methods are used for multithreading in the program.
Sleep method provides the exclusive option of locking the screen for a specific time duration which usually lasts from nanoseconds to milliseconds. But the wait method locks or suspends the screen until another method is invoked, thereby the time duration does not remain specific as during the use of sleep method.
They differentiate from each other in terms of specific functioning like class, method, synchronization and the duration of lock hold and release and several other parameters. Often the programmers get confused over which method could be ideal for sleep of the thread. The usage of the methods depends on requirements like the need, urgency and time period of the current thread.
References
- https://pdfs.semanticscholar.org/51fd/24ae444910fd39b117bb3044a12591f038fa.pdf
- https://dl.acm.org/doi/pdf/10.1145/289524.289572