Stay organized with collections
Save and categorize content based on your preferences.
ConditionVariable
open class ConditionVariable
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 |
Create the ConditionVariable in the default closed state.
|
Create the ConditionVariable with the given state.
|
Public methods |
open Unit |
Block the current thread until the condition is opened.
|
open Boolean |
Block the current thread until the condition is opened or until timeoutMs milliseconds have passed.
|
open Unit |
Reset the condition to the closed state.
|
open Unit |
Open the condition, and release all threads that are blocked.
|
Public constructors
ConditionVariable
ConditionVariable()
Create the ConditionVariable in the default closed state.
ConditionVariable
ConditionVariable(state: Boolean)
Create the ConditionVariable with the given state.
Pass true for opened and false for closed.
Public methods
block
open fun block(): Unit
Block the current thread until the condition is opened.
If the condition is already opened, return immediately.
block
open fun block(timeoutMs: Long): Boolean
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. |
Return |
Boolean |
true if the condition was opened, false if the call returns because of the timeout. |
close
open fun close(): Unit
Reset the condition to the closed state.
Any threads that call block() will block until someone calls open.
open
open fun open(): Unit
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](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)\n\nConditionVariable\n=================\n\n*** ** * ** ***\n\nKotlin \\|[Java](/reference/android/os/ConditionVariable \"View this page in Java\") \n\n```\nopen class ConditionVariable\n```\n\n|---|-----------------------------------|\n| [kotlin.Any](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html) ||\n| ↳ | [android.os.ConditionVariable](#) |\n\nClass that implements the condition variable locking paradigm.\n\nThis 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.\n\nThis class uses itself as the object to wait on, so if you wait() or notify() on a ConditionVariable, the results are undefined.\n\nSummary\n-------\n\n| Public constructors ||\n|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|\n| [ConditionVariable](#ConditionVariable())`()` Create the ConditionVariable in the default closed state. |\n| [ConditionVariable](#ConditionVariable(kotlin.Boolean))`(`state:` `[Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html)`)` Create the ConditionVariable with the given state. |\n\n| Public methods ||\n|-----------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| open [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | [block](#block())`()` Block the current thread until the condition is opened. |\n| open [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html) | [block](#block(kotlin.Long))`(`timeoutMs:` `[Long](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-long/index.html)`)` Block the current thread until the condition is opened or until timeoutMs milliseconds have passed. |\n| open [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | [close](#close())`()` Reset the condition to the closed state. |\n| open [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | [open](#open())`()` Open the condition, and release all threads that are blocked. |\n\nPublic constructors\n-------------------\n\n### ConditionVariable\n\nAdded in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nConditionVariable()\n```\n\nCreate the ConditionVariable in the default closed state. \n\n### ConditionVariable\n\nAdded in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nConditionVariable(state: Boolean)\n```\n\nCreate the ConditionVariable with the given state.\n\nPass true for opened and false for closed.\n\nPublic methods\n--------------\n\n### block\n\nAdded in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nopen fun block(): Unit\n```\n\nBlock the current thread until the condition is opened.\n\nIf the condition is already opened, return immediately. \n\n### block\n\nAdded in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nopen fun block(timeoutMs: Long): Boolean\n```\n\nBlock the current thread until the condition is opened or until timeoutMs milliseconds have passed.\n\nIf the condition is already opened, return immediately.\n\n| Parameters ||\n|-------------|-------------------------------------------------------------------------------------------------------------------------|\n| `timeoutMs` | [Long](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-long/index.html): the maximum time to wait in milliseconds. |\n\n| Return ||\n|------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------|\n| [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html) | true if the condition was opened, false if the call returns because of the timeout. |\n\n### close\n\nAdded in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nopen fun close(): Unit\n```\n\nReset the condition to the closed state.\n\nAny threads that call block() will block until someone calls open. \n\n### open\n\nAdded in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nopen fun open(): Unit\n```\n\nOpen the condition, and release all threads that are blocked.\n\nAny threads that later approach block() will not block unless close() is called."]]