Difference between wait() and sleep()

What is the difference between a wait() and sleep() in Threads?

Is my understanding that a wait() -ing Thread is still in running mode and uses CPU cycles but a sleep() -ing does not consume any CPU cycles correct?

Why do we have both wait() and sleep() : how does their implementation vary at a lower level?


A wait can be "woken up" by another thread calling notify on the monitor which is being waited on whereas a sleep cannot. Also a wait (and notify ) must happen in a block synchronized on the monitor object whereas sleep does not:

Object mon = ...;
synchronized (mon) {
    mon.wait();
} 

At this point the currently executing thread waits and releases the monitor. Another thread may do

synchronized (mon) { mon.notify(); }

(On the same mon object) and the first thread (assuming it is the only thread waiting on the monitor) will wake up.

You can also call notifyAll if more than one thread is waiting on the monitor - this will wake all of them up. However, only one of the threads will be able to grab the monitor (remember that the wait is in a synchronized block) and carry on - the others will then be blocked until they can acquire the monitor's lock.

Another point is that you call wait on Object itself (ie you wait on an object's monitor) whereas you call sleep on Thread .

Yet another point is that you can get spurious wakeups from wait (ie the thread which is waiting resumes for no apparent reason). You should always wait whilst spinning on some condition as follows:

synchronized {
    while (!condition) { mon.wait(); }
}

还没有提到的一个关键区别是,在休眠时,线程不会释放它所持有的锁,而在等待释放时, wait()对象上的锁被调用。

synchronized(LOCK) {
    Thread.sleep(1000); // LOCK is held
}


synchronized(LOCK) {
    LOCK.wait(); // LOCK is not held
}

I found this link helpful (which references this post). It puts the difference between sleep() , wait() , and yield() in human terms. (in case the links ever go dead I've included the post below with additional markup)

It all eventually makes its way down to the OS's scheduler, which hands out timeslices to processes and threads.

sleep(n) says “I'm done with my timeslice, and please don't give me another one for at least n milliseconds.” The OS doesn't even try to schedule the sleeping thread until requested time has passed.

yield() says “I'm done with my timeslice, but I still have work to do.” The OS is free to immediately give the thread another timeslice, or to give some other thread or process the CPU the yielding thread just gave up.

.wait() says “I'm done with my timeslice. Don't give me another timeslice until someone calls notify().” As with sleep() , the OS won't even try to schedule your task unless someone calls notify() (or one of a few other wakeup scenarios occurs).

Threads also lose the remainder of their timeslice when they perform blocking IO and under a few other circumstances. If a thread works through the entire timeslice, the OS forcibly takes control roughly as if yield() had been called, so that other processes can run.

You rarely need yield() , but if you have a compute-heavy app with logical task boundaries, inserting a yield() might improve system responsiveness (at the expense of time — context switches, even just to the OS and back, aren't free). Measure and test against goals you care about, as always.

链接地址: http://www.djcxy.com/p/2686.html

上一篇: HashMap,LinkedHashMap和TreeMap之间的区别

下一篇: wait()和sleep()之间的区别