I need to use a condition variable in my code. Looking up the android API, I saw that the package android.os contained ConditionVariable. But I also found Condition under java.util.concurrent.locks.
The two classes seem to be designed for the same purpose.
Condition.await() <-> ConditionVariable.block()
Condition.signal() <-> ConditionVariable.open()
Is there a difference I should be aware of?
As described in the docs you linked,
Condition
can wake single Threads, instead of waking all waiting ones (signal
vssignalAll
).ConditionVariable
is state-based and thus allows keeping the condition open (in contrast to releasing all waiting threads at a single time, it is possible to not block threads at all usingopen
andclose
).So you should use
Condition
ifConditionVariable
is not available there.ConditionVariable
, as a single call tosignalAll
is nicer to read thanopen
directly followed byclose
.while you should use
ConditionVariable
if