The method sleep and wait sounds like doing the same task, but they are much different from each other. Sleep method belongs to the Thread class, and the wait method belongs to the Object class. The most important difference that distinguishes both of them is, the sleep method holds the lock on the object till it is interrupted or it finishes its execution.
On the other hand, the wait method releases the lock on the object to let the other objects execute till is resumed by the notify method. There are some more differences between sleep and wait method; you can explore them in the comparison chart shown below.
Content: Sleep Vs Wait Method
|Basis for Comparison||sleep||wait|
|Basic||Sleep method does not release the lock on an object until it is interrupted.||Wait method releases the lock on the object to let other objects execute till notify or notifyAll method is invoked.|
|Class||Sleep method is defined in Thread class.||Wait method is defined in the Object class.|
|Synchronized||Sleep method need not be called within Synchronized context.||Wait method has to be called within the Synchronized context.|
|Completion||Sleep method execution is completed when a thread interrupts it or the time of sleep method is expired.||Wait method execution is completed when notify() or notifyAll() method interrupt it.|
|Static||Sleep method is a static method.||Wait method is not static that's why it need object to invoke it.|
|Execution||The sleep method is executed on the current thread.||The wait method is executed on the object.|
|Exception||InterruptedException||IllegalArgumentException, IllegalMonitorStateException, InterruptedException.|
Definition of Sleep Method
The sleep method is the static method of the Thread class. The sleep method is invoked when a thread does not want to perform any task for a particular amount of time. The sleep method completes its execution either when the time of the method is expired, or it is interrupted by some other thread in the execution. After the sleep method has completed, the thread again gains its runnable status. There are two from of the sleep method as shown below.
public static native void sleep(long millisecond); public static void sleep(long milliseconds and int nanosecond);
In the above two form, you can observe time is mentioned in both the form of the sleep method. This is because a thread always sleeps for a particular time mentioned in the parameter. The sleep method can be interrupted by any other thread during its execution; hence, it can throw the InterruptedExecption.
You must take care that the exception is handled either by try/catch block or by Throws keywords otherwise, it will lead to the compile time error. The sleep method holds the lock on the current object in the execution and doesn’t allow any other object to execute till it finishes its execution.
Definition of Wait Method
Wait is the method defined in the Object class. The wait method is used when several tasks (threads) are struggling for the same resource one at a time. The wait method when invoked it releases the lock on the object that has invoked the wait method. And provide a chance to another object to get executed till a notify method is invoked which further resume the lock on the object released by wait method. The wait method has the following form.
wait(); wait(long millisecond); wait(long millisecond, int nanosecond);
As in the above three forms of the wait method, the first method has no time parameter it means the execution of the wait method will go on till the notify, or notifyAll method is not invoked. The rest two methods have time parameters. So they will execute till the time expires or till notify or noitfyAll method is invoked earlier than the time expires.
The wait method can throw exceptions. IllegalArgumentException, if the time argument passed is negative. IllegalMonitorStateException, if the current thread is not using the resource. InterruptedException, if the wait method is interrupted.
Key Differences Between Sleep and Wait Method in Java
- The main point that distinguishes the sleep and wait method is that sleep method holds the lock on the object till it is interrupted or its time expires. However, the wait method releases the lock on the object that invokes it and gives other objects the chance to execute.
- The sleep method is defined in the Thread class whereas, the wait method is defined in the Object class.
- The sleep method need not be called from synchronised context. But the wait method needs to be called from synchronised.
- The sleep method is awakened if the time of the method is expired or it is interrupted by the other thread. The wait method is awakened
when the notify or notifyAll method is invoked.
- The sleep method is static method whereas, the wait method is not a static method.
- The sleep method is executed on the current thread whereas, the wait method is executed on the object that invokes it.
- The exception thrown by sleep method is InterruptedException whereas the exceptions thrwon by wait method are IllegalArgumentException, IllegalMonitorStateException, InterruptedException.
When multiple threads are wants to use the same resource one by one, then the wait method must be used. When a thread does not want to perform any task, then sleep method must be used.
Leave a Reply