8

I came across a question in which the poster tried to have a thread wait for a second. They were using wait, but outside a synchronized block, and therefore it crashed.

Given a running thread, to pause the execution for a given time, one would do:

Thread.sleep(1000);

This should work as well, and have very similar result:

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

Using the wait timeout, the thread will unpause 1 second later.

The question is this: if I don't have any monitoring and notifying issue, is there an actual reason to use one over the other?

njzk2
  • 38,969
  • 7
  • 69
  • 107
  • 1
    Helpful link on the matter: http://stackoverflow.com/questions/1036754/difference-between-wait-and-sleep – Ivan Apr 02 '14 at 22:49
  • @Ivan : Yes, I have read this question, there are discussions regarding this question is comments, but I didn't really find a definite answer. – njzk2 Apr 02 '14 at 22:51
  • 2
    The answer is that they do different things. sleep() is subject to interrupts. wait() is subject to notify(). They're not equivalent. – user207421 Apr 02 '14 at 23:20
  • 1
    `wait` and `sleep` present completely different functionality. This frankly seems like a duplicate of that other question. How is it not? The two top answers over there show the differences pretty clearly. – Radiodef Apr 03 '14 at 00:17
  • I know the difference between the 2. But the appear to be able to perform the same task in some cases (pausing the execution of a thread.) In the linked question there are discussions, but no actual answer to this. – njzk2 Apr 03 '14 at 13:10
  • @assylias posted a quite interesting answer, that was downvoted and removed. there is a semantic value to the use of sleep or wait, in the sense that wait is expected to be used in conjunction with notify – njzk2 Apr 03 '14 at 13:35

2 Answers2

5

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

sleep() is usually used when you know exactly how long you want your thread to be inactive. After the given timeout, it will wake up automatically, without interference from outside. There's still a chance that someone will decide to wake up your thread earlier if something urgent happens (in this case, the call to sleep() will end up with an InterruptedException). For instance, the user has decided to close the application when the thread was asleep, or something like this.

So, sleep() is like setting an alarm clock to wake you up in an hour while going to doze. But someone can wake you up earlier to say that the building is on fire and it's better to get up and do something about it.

wait(), on the other hand, is designed to put a thread on hold until something happens sometime in the future. You don't know how long it will take. There must be someone outside who will wake up the thread by calling notify() or notifyAll() on the monitor (on the same object that was used to call wait()). For instance, a thread has delegated some job to another thread and wants to sleep until the job is done. You also have an option to limit the waiting time, but the thread won't continue execution until it can reacquire the monitor. The waiting thread is still can be interrupted the same way as with the sleep().

So, wait() is like you have the only screwdriver in the workshop, lend it to your coworker for a while and decide to doze a bit until he or she has finished. You ask them to wake you up when your screwdriver is free again and you can continue your job. You can also set an alarm clock like in sleep(), but you won't be able to get back to work until you get your screwdriver back.

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

Andrew Lygin
  • 6,077
  • 1
  • 32
  • 37
0

The difference is pretty clear in the javadoc:

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

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

static void Thread.sleep(long millis): Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers.

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

Community
  • 1
  • 1
Naili
  • 1,564
  • 3
  • 20
  • 27
  • Thank you for your answer. I know the difference between the 2. I wonder if, since both can be used to achieve the purpose of pausing the thread, there is a reason for using one over the other. (when there is no constraint that forces the use of one, such a monitors being held). – njzk2 Apr 03 '14 at 13:09