From java doc 1.8, seems Condition only has await()
, await(long, TimeUnit)
, awaitNanos()
, etc.
It doesn't have a function that looks like Condition.await(()->xxxxx) to wait until a condition is met. c 11 condition_variable supports to have cv.wait(mutex, predicate_function)
so we can use a lambda function as a parameter to control when condition_variable should return from wait/await.
Does java support this?
CodePudding user response:
From the docs, the C condition_variable::wait
overload is just checking the predicate when a notification comes through and is equivalent to
while (!stop_waiting()) { wait(lock); }
So we can simply do the same thing in Java.
while (!someCondition()) {
conditionVariable.await();
}