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.
Is there any way to block the Thread, until the boolean value changes it state?
This question is related to
java
multithreading
boolean
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 :
//.....some class
volatile boolean someBoolean;
Thread someThread = new Thread() {
@Override
public void run() {
//some actions
while (!someBoolean); //wait for condition
//some actions
}
};
How about 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();
}
}
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
:
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.
public Boolean test() throws InterruptedException {
BlockingQueue<Boolean> booleanHolder = new LinkedBlockingQueue<>();
new Thread(() -> {
try {
TimeUnit.SECONDS.sleep(2);
booleanHolder.put(true);
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
return booleanHolder.poll(4, TimeUnit.SECONDS);
}
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.
Integer any = new Integer(0);
public synchronized boolean waitTillChange() {
any.wait();
return true;
}
public synchronized void change() {
any.notify();
}
Source: Stackoverflow.com