Can I use wait instead of sleep?

I came across a question in which the poster tried to stream for a second. They used wait , but outside the synchronized block, and so it crashed.

Given the current thread, to pause execution for a given time, follow these steps:

 Thread.sleep(1000); 

This should also work and have a very similar result:

 synchronized(this) { this.wait(1000); } 

Using the wait timeout, the thread will be disconnected 1 second later.

The question is: if I do not have any monitoring and notification problems, is there an actual reason to use it over another?

+8
java multithreading
source share
2 answers

Both sleep() and wait() are used to hold the current thread, but they were designed for different use cases:

sleep() usually used when you know exactly how long you want your thread to be inactive. After a specified timeout, it will automatically wake up, without external interference. There is still a chance that someone decides to wake up your topic earlier if something urgent happens (in this case, the sleep() call will end with an InterruptedException ). For example, the user decided to close the application when the thread was sleeping, or something like this.

So, sleep() is like setting an alarm to wake you up in an hour while you were dozing. But someone may wake you up earlier to say that the building is on fire, and it is better to get up and do something about it.

wait() , on the other hand, is designed to hold the thread until something happens in the future. You do not know how long it will take. There must be someone outside who will wake the thread by calling notify() or notifyAll() on the monitor (on the same object that was used to call wait() ). For example, a thread has delegated some work to another thread and wants to sleep until the work is completed. You also have the option of limiting the wait time, but the thread will not continue executing until it can re-capture the monitor. The waiting thread can still be interrupted just like with sleep() .

So, wait() like you have a single screwdriver in the workshop, give it to your colleague for a while and decide to come around a bit until he or she finishes. You ask them to wake you when your screwdriver is free again, and you can continue your work. You can also set the alarm as in sleep() , but you cannot return to work until you return the screwdriver.

Of course, these are the usual simple approaches to using methods. You can develop your own usage scenarios based on their functionality.

+4
source share

The difference is pretty clear in javadoc :

void Object.wait() : Causes the current thread to wait until another thread calls the notify() or notifyAll() method for this object.

void Object.wait(long timeout) : Causes the current thread to wait until another thread calls the notify() method or the notifyAll() method for this object, or the specified amount of time expires.

static void Thread.sleep(long millis) : Causes the current executable to sleep (temporarily stop execution) for the specified number of milliseconds, taking into account the accuracy and accuracy of system timers and schedulers.

Otherwise, the question is asked and gets an explained answer here .

+1
source share

All Articles