Equivalent to wait (x == 0) in Java
2 answers
If it's a one-off event, you can use aCountDownLatch
:
private final CountDownLatch xIsZeroLatch = new CountDownLatch(1);
Then you use it like this:
-
in a waiting thread:
xIsZeroLatch.await();
-
in the other thread (s):
x = newX(); if (x == 0) xIsZeroLatch.countDown();
If a condition can change multiple times between true and false, and each change requires an event, you can use Semaphore
with one resolution.
+4
source to share