ConditionVariable
@UnstableApi
class ConditionVariable
An interruptible condition variable. This class provides a number of benefits over android.os.ConditionVariable:
- Consistent use of (
elapsedRealtimefor timingblocktimeout intervals.android.os.ConditionVariableusedcurrentTimeMillisprior 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. openandclosereturn whether they changed the variable's state.
Summary
Public constructors |
|---|
|
Creates a closed instance using |
ConditionVariable(clock: Clock!)Creates an instance, which starts closed. |
Public functions |
|
|---|---|
synchronized Unit |
block()Blocks until the condition is opened. |
synchronized Boolean |
Blocks until the condition is opened or until |
synchronized Unit |
Blocks until the condition is open. |
synchronized Boolean |
blockUninterruptible(timeoutMs: Long)Blocks until the condition is open or until |
synchronized Boolean |
close()Closes the condition. |
synchronized Boolean |
open()Opens the condition and releases all threads that are blocked. |
Public constructors
ConditionVariable
ConditionVariable(clock: Clock!)
Creates an instance, which starts closed.
| Parameters | |
|---|---|
clock: Clock! |
The |
Public functions
block
synchronized fun block(): Unit
Blocks until the condition is opened.
| Throws | |
|---|---|
java.lang.InterruptedException |
If the thread is interrupted. |
block
synchronized fun block(timeoutMs: Long): Boolean
Blocks until the condition is opened or until timeoutMs have passed.
| Parameters | |
|---|---|
timeoutMs: Long |
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 fun blockUninterruptible(): Unit
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.
blockUninterruptible
synchronized fun blockUninterruptible(timeoutMs: Long): Boolean
Blocks until the condition is open or until timeoutMs have passed.
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.
| Parameters | |
|---|---|
timeoutMs: Long |
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. |