The difference between Wait and sleep in Java
In this article, we’ll discuss the differences between wait() and sleep() methods in Java. And discuss how to use these two methods.
The difference between ‘Wait’ and ‘sleep
Wait () is a native method defined in Object:
public final native void wait(long timeout) throws InterruptedException;
Copy the code
So every instance of a class can call this method. Wait () can only be called in synchronized blocks. It releases the lock placed on an object by synchronized.
Sleep () is a native static class method that defines Thread:
public static native void sleep(long millis) throws InterruptedException;
Copy the code
So thread.sleep () can be called in any case. Thread.sleep() will suspend the current Thread and will not release any lock resources.
Let’s look at a simple use of wait:
@Slf4j
public class WaitUsage {
private static Object LOCK = new Object();
public static void WaitExample(a) throws InterruptedException {
synchronized (LOCK) {
LOCK.wait(1000);
log.info("Object '" + LOCK + "' is woken after" +
" waiting for 1 second"); }}}Copy the code
Take another look at the use of sleep:
@Slf4j
public class SleepUsage {
public static void sleepExample(a) throws InterruptedException {
Thread.sleep(1000);
log.info(
"Thread '" + Thread.currentThread().getName() +
"' is woken after sleeping for 1 second"); }}Copy the code
Wake up “wait” and “sleep”
The sleep() method has its own sleep time, after which the Thread is automatically woken up. Or you can interrupt it by calling the interrupt() method.
To wake up a wait, we call notify() and notifyAll() to wake up threads waiting on a particular Wait object.
NotifyAll () awakens all waiting threads, and notifyAll() awakens all waiting threads, which recompete for the resource lock.
Wait,notity is used in the producer and consumer situation. Let’s see how it is used:
@Slf4j
public class WaitNotifyUsage {
private int count =0;
public void produceMessage(a) throws InterruptedException {
while(true) {
synchronized (this) {
while (count == 5) {
log.info("count == 5 , wait ....");
wait();
}
count++;
log.info("produce count {}", count); notify(); }}}public void consumeMessage(a) throws InterruptedException {
while (true) {
synchronized (this) {
while (count == 0) {
log.info("count == 0, wait ...");
wait();
}
log.info("consume count {}", count); count--; notify(); }}}}Copy the code
See how to call:
@Test
public void testWaitNotifyUsage(a) throws InterruptedException{
WaitNotifyUsage waitNotifyUsage=new WaitNotifyUsage();
ExecutorService executorService=Executors.newFixedThreadPool(4);
executorService.submit(()-> {
try {
waitNotifyUsage.produceMessage();
} catch(InterruptedException e) { e.printStackTrace(); }}); executorService.submit(()-> {try {
waitNotifyUsage.consumeMessage();
} catch(InterruptedException e) { e.printStackTrace(); }}); Thread.sleep(50000);
}
Copy the code
Examples of this article can be found at github.com/ddean2009/l…
See flydean’s blog for more tutorials