java 等待()/等待(超时)/睡眠(超时)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3200402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
wait()/wait(timeout)/sleep(timeout)?
提问by JavaUser
What is the difference between wait() and wait(timeout).Anyway wait() needs to wait for a notify call but why we have wait(timeout) ?
wait() 和 wait(timeout) 有什么区别?反正 wait() 需要等待一个通知调用,但为什么我们有 wait(timeout) ?
So what is the difference between sleep(timeout) and wait(timeout) ?
那么 sleep(timeout) 和 wait(timeout) 之间有什么区别?
回答by Borealid
wait(timeout) will return if the thread is still waiting after the timeout has elapsed. This is for hang notification, for low-power polling, etc etc. Sleep(timeout) won'twake up until the timeout has elapsed; wait(timeout) is either the notify() call or the timeout, whichever comes first.
如果超时后线程仍在等待,wait(timeout) 将返回。这是用于挂起通知、低功耗轮询等。 Sleep(timeout)在超时过去之前不会唤醒;wait(timeout) 是 notify() 调用或超时,以先到者为准。
Quoting from the JavaDoc:
引用 JavaDoc:
This method causes the current thread (call it T) to place itself in the wait set for this object and then to relinquish any and all synchronization claims on this object. Thread T becomes disabled for thread scheduling purposes and lies dormant until one of four things happens:
- Some other thread invokes the notify method for this object and thread T happens to be arbitrarily chosen as the thread to be awakened.
- Some other thread invokes the notifyAll method for this object.
- Some other thread interrupts thread T.
- The specified amount of real time has elapsed, more or less. If timeout is zero, however, then real time is not taken into consideration and the thread simply waits until notified.
此方法使当前线程(称为 T)将自己置于此对象的等待集中,然后放弃对该对象的任何和所有同步声明。线程 T 出于线程调度目的而被禁用并处于休眠状态,直到发生以下四种情况之一:
- 某个其他线程为此对象调用了notify 方法,而线程T 恰好被任意选择为要唤醒的线程。
- 其他一些线程为此对象调用notifyAll 方法。
- 其他一些线程中断了线程 T。
- 或多或少已经过了指定的实时时间。但是,如果超时为零,则不考虑实时时间,线程只是等待直到收到通知。
回答by xagyg
wait(timeout): when the timeout expires the thread wakes up and tries to re-acquire the synchronization lock (i.e. if another thread has not notified it within the timeout period).
wait(timeout):当超时到期时线程唤醒并尝试重新获取同步锁(即如果另一个线程在超时时间内没有通知它)。
sleep(timeout): sleep can be used without any sycnhronization code - it just puts the thread to sleep for the specified timeout. Note that wait must be used within synchronized code.
sleep(timeout): sleep 可以在没有任何同步代码的情况下使用 - 它只是让线程在指定的超时时间内休眠。请注意,必须在同步代码中使用等待。
So, wait is used when you expect a thread to be notified by another thread (but may not, hence timeout). And, wait must be called within synchronized code.
因此,当您希望一个线程被另一个线程通知时使用等待(但可能不会,因此超时)。并且,必须在同步代码中调用wait。
回答by MKA
The only difference between wait()and wait(timeout)is that wait()will neverwake up without a notify(). wait(timeout)with a timeout > 0 allows you to potentially save yourself from locking up your application "forever" if a call to notify()never occurs.
唯一的区别wait(),并wait(timeout)在于wait()将永远不会醒来没有notify()。wait(timeout)超时 > 0 允许您在调用notify()从不发生时避免“永远”锁定您的应用程序。
回答by jedzej
The difference is that these methods are designed for different purposes. Sleep is used for introducing unconditional delay in thread execution, whilst wait is used for synchronization between threads, e.g.:
不同之处在于这些方法是为不同的目的而设计的。睡眠用于在线程执行中引入无条件延迟,而等待用于线程之间的同步,例如:
If you want to implement countdown, delay, timeryou should use sleep(t).
If you have 2 (or more) threads, where 1st is a producer which spawns data randomly, whilst 2nd is consumer which consumes what 1st one provides, you should go for wait()/notify()pattern or consider modern more convenient wrappers for this threads synchronization if it's present on your platform.
If you have case 2, but you want to add a timeout to fire after certain time instead of hanging forever in wait(), you go for wait(t)instead of wait().
如果你想实现倒计时、延迟、计时器,你应该使用sleep(t)。
如果您有 2 个(或更多)线程,其中第一个是随机生成数据的生产者,而第二个是消耗第一个提供的数据的消费者,您应该使用wait()/notify()模式或考虑现代更方便的包装器如果您的平台上存在此线程同步。
如果您有第 2 种情况,但您想在特定时间后添加超时触发而不是永远挂在 wait() 中,则使用wait(t)而不是wait()。
In fact if you never notify, wait(t) acts similarly to sleep(t) - it's gonna return after timeout, so you could use it for case 1, but it's much more clunky and ugly.
事实上,如果你从不通知,wait(t) 的行为与 sleep(t) 类似——它会在超时后返回,所以你可以将它用于案例 1,但它更笨拙和丑陋。

