ConditionVariable
@UnstableApi
public class ConditionVariable
An interruptible condition variable. This class provides a number of benefits over :
- Consistent use of (
elapsedRealtime
for timingblock
timeout intervals.android.os.ConditionVariable
usedcurrentTimeMillis
prior to Android 10, which is not a correct clock to use for interval timing because it's not guaranteed to be monotonic. - Support for injecting a custom
Clock
. - The ability to query the variable's current state, by calling
isOpen
. open
andclose
return whether they changed the variable's state.
Summary
Public fields |
|
---|---|
boolean |
Public constructors |
---|
Creates a closed instance using |
ConditionVariable(Clock clock) Creates an instance, which starts closed. |
Public methods |
|
---|---|
synchronized void |
block() Blocks until the condition is opened. |
synchronized boolean |
block(long timeoutMs) Blocks until the condition is opened or until |
synchronized void |
Blocks until the condition is open. |
synchronized boolean |
close() Closes the condition. |
synchronized boolean |
isOpen() Returns whether the condition is opened. |
synchronized boolean |
open() Opens the condition and releases all threads that are blocked. |
Public fields
Public constructors
ConditionVariable
public ConditionVariable(Clock clock)
Creates an instance, which starts closed.
Parameters | |
---|---|
Clock clock |
The |
Public methods
block
synchronized public void block()
Blocks until the condition is opened.
Throws | |
---|---|
java.lang.InterruptedException |
If the thread is interrupted. |
block
synchronized public boolean block(long timeoutMs)
Blocks until the condition is opened or until timeoutMs
have passed.
Parameters | |
---|---|
long timeoutMs |
The maximum time to wait in milliseconds. If |
Returns | |
---|---|
boolean |
True if the condition was opened, false if the call returns because of the timeout. |
Throws | |
---|---|
java.lang.InterruptedException |
If the thread is interrupted. |
blockUninterruptible
synchronized public void blockUninterruptible()
Blocks until the condition is open. Unlike block, this method will continue to block if the calling thread is interrupted. If the calling thread was interrupted then its interrupted status
will be set when the method returns.
close
synchronized public boolean close()
Closes the condition.
Returns | |
---|---|
boolean |
True if the condition variable was closed. False if it was already closed. |
open
synchronized public boolean open()
Opens the condition and releases all threads that are blocked.
Returns | |
---|---|
boolean |
True if the condition variable was opened. False if it was already open. |