Java 等到布尔值改变它的状态
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19025366/
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 until boolean value changes it state
提问by Marcel H?ll
I have a thread which wait for a boolean value to change like this:
我有一个线程等待布尔值像这样改变:
while(!value)
{
Thread.sleep(1000)
}
// Do some work after change of the value
This is not my prefered way to do this, cause of massive CPU consumption.
这不是我的首选方式,因为会消耗大量 CPU。
Is there any way to block the Thread, until the boolean value changes it state?
有没有办法阻塞线程,直到布尔值改变它的状态?
采纳答案by Michael Borgwardt
This is not my prefered way to do this, cause of massive CPU consumption.
这不是我的首选方式,因为会消耗大量 CPU。
If that is actually your working code, then just keep it like that. Checking a boolean once a second causes NO measurable CPU load. None whatsoever.
如果这实际上是您的工作代码,那么就保持原样。每秒检查一次布尔值不会导致可测量的 CPU 负载。没有任何。
The real problem is that the thread that checks the value may not see a change that has happened for an arbitrarily long time due to caching. To ensure that the value is always synchronized between threads, you need to put the volatile keyword in the variable definition, i.e.
真正的问题是检查值的线程可能看不到由于缓存而发生的任意长时间的更改。为保证线程间的值始终同步,需要在变量定义中放入 volatile 关键字,即
private volatile boolean value;
Note that putting the access in a synchronized
block, such as when using the notification-based solution described in other answers, will have the same effect.
请注意,将访问放在一个synchronized
块中,例如在使用其他答案中描述的基于通知的解决方案时,将具有相同的效果。
回答by Subhrajyoti Majumder
How about wait-notify
怎么样 wait-notify
private Boolean bool = true;
private final Object lock = new Object();
private Boolean getChange(){
synchronized(lock){
while (bool) {
bool.wait();
}
}
return bool;
}
public void setChange(){
synchronized(lock){
bool = false;
bool.notify();
}
}
回答by Marko Topolnik
You need a mechanism which avoids busy-waiting. The old wait/notify
mechanism is fraught with pitfalls so prefer something from the java.util.concurrent
library, for example the CountDownLatch
:
您需要一种避免忙等待的机制。旧wait/notify
机制充满陷阱,因此更喜欢java.util.concurrent
库中的某些内容,例如CountDownLatch
:
public final CountDownLatch latch = new CountDownLatch(1);
public void run () {
latch.await();
...
}
And at the other side call
在另一边打电话
yourRunnableObj.latch.countDown();
However, starting a thread to do nothing but wait until it is needed is still not the best way to go. You could also employ an ExecutorService
to which you submit as a task the work which must be done when the condition is met.
然而,启动一个线程什么也不做,直到需要它仍然不是最好的方法。您还可以使用ExecutorService
一个任务,将满足条件时必须完成的工作作为任务提交。
回答by Michael Borgwardt
I prefer to use mutex mechanism in such cases, but if you really want to use boolean, then you should declare it as volatile (to provide the change visibility across threads) and just run the body-less cycle with that boolean as a condition :
在这种情况下,我更喜欢使用互斥机制,但是如果您真的想使用布尔值,那么您应该将其声明为 volatile(以提供跨线程的更改可见性),并且只需使用该布尔值作为条件运行无体循环:
//.....some class
volatile boolean someBoolean;
Thread someThread = new Thread() {
@Override
public void run() {
//some actions
while (!someBoolean); //wait for condition
//some actions
}
};
回答by luiso1979
Ok maybe this one should solve your problem. Note that each time you make a change you call the change() method that releases the wait.
好吧,也许这个应该可以解决您的问题。请注意,每次进行更改时都会调用 change() 方法来释放等待。
Integer any = new Integer(0);
public synchronized boolean waitTillChange() {
any.wait();
return true;
}
public synchronized void change() {
any.notify();
}