Answer by rdesgroppes for is there a 'block until condition becomes true'...
One could also leverage CompletableFutures (since Java 8):final CompletableFuture<String> question = new CompletableFuture<>();// from within the consumer thread:final String answer =...
View ArticleAnswer by borjab for is there a 'block until condition becomes true' function...
As nobody published a solution with CountDownLatch. What about:public class Lockeable { private final CountDownLatch countDownLatch = new CountDownLatch(1); public void doAfterEvent(){...
View ArticleAnswer by axblount for is there a 'block until condition becomes true'...
Lock-free solution(?)I had the same issue, but I wanted a solution that didn't use locks.Problem: I have at most one thread consuming from a queue. Multiple producer threads are constantly inserting...
View ArticleAnswer by Solomon Slow for is there a 'block until condition becomes true'...
EboMike's answer and Toby's answer are both on the right track, but they both contain a fatal flaw. The flaw is called lost notification.The problem is, if a thread calls foo.notify(), it will not do...
View ArticleAnswer by Toby for is there a 'block until condition becomes true' function...
Similar to EboMike's answer you can use a mechanism similar to wait/notify/notifyAll but geared up for using a Lock.For example,public void doSomething() throws InterruptedException { lock.lock(); try...
View ArticleAnswer by Jérôme Verstrynge for is there a 'block until condition becomes...
You could use a semaphore.While the condition is not met, another thread acquires the semaphore.Your thread would try to acquire it with acquireUninterruptibly()or tryAcquire(int permits, long timeout,...
View ArticleAnswer by EboMike for is there a 'block until condition becomes true'...
Polling like this is definitely the least preferred solution.I assume that you have another thread that will do something to make the condition true. There are several ways to synchronize threads. The...
View Articleis there a 'block until condition becomes true' function in java?
I'm writing a listener thread for a server, and at the moment I'm using:while (true){ try { if (condition){ //do something condition=false; } sleep(1000); } catch (InterruptedException ex){...
View Article
More Pages to Explore .....