Stay organized with collections
Save and categorize content based on your preferences.
ConditionVariable
public
class
ConditionVariable
extends Object
Class that implements the condition variable locking paradigm.
This differs from the built-in java.lang.Object wait() and notify()
in that this class contains the condition to wait on itself. That means
open(), close() and block() are sticky. If open() is called before block(),
block() will not block, and instead return immediately.
This class uses itself as the object to wait on, so if you wait()
or notify() on a ConditionVariable, the results are undefined.
Summary
Public constructors |
ConditionVariable()
Create the ConditionVariable in the default closed state.
|
ConditionVariable(boolean state)
Create the ConditionVariable with the given state.
|
Public methods |
void
|
block()
Block the current thread until the condition is opened.
|
boolean
|
block(long timeoutMs)
Block the current thread until the condition is opened or until
timeoutMs milliseconds have passed.
|
void
|
close()
Reset the condition to the closed state.
|
void
|
open()
Open the condition, and release all threads that are blocked.
|
Inherited methods |
From class
java.lang.Object
Object
|
clone()
Creates and returns a copy of this object.
|
boolean
|
equals(Object obj)
Indicates whether some other object is "equal to" this one.
|
void
|
finalize()
Called by the garbage collector on an object when garbage collection
determines that there are no more references to the object.
|
final
Class<?>
|
getClass()
Returns the runtime class of this Object .
|
int
|
hashCode()
Returns a hash code value for the object.
|
final
void
|
notify()
Wakes up a single thread that is waiting on this object's
monitor.
|
final
void
|
notifyAll()
Wakes up all threads that are waiting on this object's monitor.
|
String
|
toString()
Returns a string representation of the object.
|
final
void
|
wait(long timeoutMillis, int nanos)
Causes the current thread to wait until it is awakened, typically
by being notified or interrupted, or until a
certain amount of real time has elapsed.
|
final
void
|
wait(long timeoutMillis)
Causes the current thread to wait until it is awakened, typically
by being notified or interrupted, or until a
certain amount of real time has elapsed.
|
final
void
|
wait()
Causes the current thread to wait until it is awakened, typically
by being notified or interrupted.
|
|
Public constructors
ConditionVariable
public ConditionVariable ()
Create the ConditionVariable in the default closed state.
ConditionVariable
public ConditionVariable (boolean state)
Create the ConditionVariable with the given state.
Pass true for opened and false for closed.
Public methods
block
public void block ()
Block the current thread until the condition is opened.
If the condition is already opened, return immediately.
block
public boolean block (long timeoutMs)
Block the current thread until the condition is opened or until
timeoutMs milliseconds have passed.
If the condition is already opened, return immediately.
Parameters |
timeoutMs |
long : the maximum time to wait in milliseconds. |
Returns |
boolean |
true if the condition was opened, false if the call returns
because of the timeout. |
close
public void close ()
Reset the condition to the closed state.
Any threads that call block() will block until someone calls open.
open
public void open ()
Open the condition, and release all threads that are blocked.
Any threads that later approach block() will not block unless close()
is called.
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-02-10 UTC."],[],[],null,["# ConditionVariable\n\nAdded in [API level 1](/guide/topics/manifest/uses-sdk-element#ApiLevels) \nSummary: [Ctors](#pubctors) \\| [Methods](#pubmethods) \\| [Inherited Methods](#inhmethods) \n\nConditionVariable\n=================\n\n*** ** * ** ***\n\n[Kotlin](/reference/kotlin/android/os/ConditionVariable \"View this page in Kotlin\") \\|Java\n\n\n`\npublic\n\n\nclass\nConditionVariable\n`\n\n\n`\n\nextends `[Object](/reference/java/lang/Object)`\n\n\n`\n\n`\n\n\n`\n\n|---|------------------------------|\n| [java.lang.Object](/reference/java/lang/Object) ||\n| ↳ | android.os.ConditionVariable |\n\n\u003cbr /\u003e\n\n*** ** * ** ***\n\nClass that implements the condition variable locking paradigm.\n\n\nThis differs from the built-in java.lang.Object wait() and notify()\nin that this class contains the condition to wait on itself. That means\nopen(), close() and block() are sticky. If open() is called before block(),\nblock() will not block, and instead return immediately.\n\n\nThis class uses itself as the object to wait on, so if you wait()\nor notify() on a ConditionVariable, the results are undefined.\n\nSummary\n-------\n\n| ### Public constructors ||\n|-----------------------------------------------------------------------------------------------------------------------------------------------------------------|---|\n| ` `[ConditionVariable](/reference/android/os/ConditionVariable#ConditionVariable())`() ` Create the ConditionVariable in the default closed state. |\n| ` `[ConditionVariable](/reference/android/os/ConditionVariable#ConditionVariable(boolean))`(boolean state) ` Create the ConditionVariable with the given state. |\n\n| ### Public methods ||\n|------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| ` void` | ` `[block](/reference/android/os/ConditionVariable#block())`() ` Block the current thread until the condition is opened. |\n| ` boolean` | ` `[block](/reference/android/os/ConditionVariable#block(long))`(long timeoutMs) ` Block the current thread until the condition is opened or until timeoutMs milliseconds have passed. |\n| ` void` | ` `[close](/reference/android/os/ConditionVariable#close())`() ` Reset the condition to the closed state. |\n| ` void` | ` `[open](/reference/android/os/ConditionVariable#open())`() ` Open the condition, and release all threads that are blocked. |\n\n| ### Inherited methods |\n|-----------------------|---|\n| From class ` `[java.lang.Object](/reference/java/lang/Object)` ` |---------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` `[Object](/reference/java/lang/Object) | ` `[clone](/reference/java/lang/Object#clone())`() ` Creates and returns a copy of this object. | | ` boolean` | ` `[equals](/reference/java/lang/Object#equals(java.lang.Object))`(`[Object](/reference/java/lang/Object)` obj) ` Indicates whether some other object is \"equal to\" this one. | | ` void` | ` `[finalize](/reference/java/lang/Object#finalize())`() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. | | ` final `[Class](/reference/java/lang/Class)`\u003c?\u003e` | ` `[getClass](/reference/java/lang/Object#getClass())`() ` Returns the runtime class of this `Object`. | | ` int` | ` `[hashCode](/reference/java/lang/Object#hashCode())`() ` Returns a hash code value for the object. | | ` final void` | ` `[notify](/reference/java/lang/Object#notify())`() ` Wakes up a single thread that is waiting on this object's monitor. | | ` final void` | ` `[notifyAll](/reference/java/lang/Object#notifyAll())`() ` Wakes up all threads that are waiting on this object's monitor. | | ` `[String](/reference/java/lang/String) | ` `[toString](/reference/java/lang/Object#toString())`() ` Returns a string representation of the object. | | ` final void` | ` `[wait](/reference/java/lang/Object#wait(long,%20int))`(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` `[wait](/reference/java/lang/Object#wait(long))`(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` `[wait](/reference/java/lang/Object#wait())`() ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*. | ||\n\nPublic constructors\n-------------------\n\n### ConditionVariable\n\nAdded in [API level 1](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\n```\npublic ConditionVariable ()\n```\n\nCreate the ConditionVariable in the default closed state.\n\n\u003cbr /\u003e\n\n### ConditionVariable\n\nAdded in [API level 1](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\n```\npublic ConditionVariable (boolean state)\n```\n\nCreate the ConditionVariable with the given state.\n\n\nPass true for opened and false for closed.\n\n\u003cbr /\u003e\n\n| Parameters ||\n|---------|------------------|\n| `state` | `boolean` \u003cbr /\u003e |\n\nPublic methods\n--------------\n\n### block\n\nAdded in [API level 1](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\n```\npublic void block ()\n```\n\nBlock the current thread until the condition is opened.\n\n\nIf the condition is already opened, return immediately.\n\n\u003cbr /\u003e\n\n### block\n\nAdded in [API level 1](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\n```\npublic boolean block (long timeoutMs)\n```\n\nBlock the current thread until the condition is opened or until\ntimeoutMs milliseconds have passed.\n\n\nIf the condition is already opened, return immediately.\n\n\u003cbr /\u003e\n\n| Parameters ||\n|-------------|----------------------------------------------------------|\n| `timeoutMs` | `long`: the maximum time to wait in milliseconds. \u003cbr /\u003e |\n\n| Returns ||\n|-----------|--------------------------------------------------------------------------------------------|\n| `boolean` | true if the condition was opened, false if the call returns because of the timeout. \u003cbr /\u003e |\n\n### close\n\nAdded in [API level 1](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\n```\npublic void close ()\n```\n\nReset the condition to the closed state.\n\n\nAny threads that call block() will block until someone calls open.\n\n\u003cbr /\u003e\n\n### open\n\nAdded in [API level 1](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\n```\npublic void open ()\n```\n\nOpen the condition, and release all threads that are blocked.\n\n\nAny threads that later approach block() will not block unless close()\nis called.\n\n\u003cbr /\u003e"]]