ConditionVariable


@UnstableApi
public class ConditionVariable


An interruptible condition variable. This class provides a number of benefits over :

Summary

Public fields

boolean

Public constructors

Creates a closed instance using DEFAULT.

Creates an instance, which starts closed.

Public methods

synchronized void

Blocks until the condition is opened.

synchronized boolean
block(long timeoutMs)

Blocks until the condition is opened or until timeoutMs have passed.

synchronized void

Blocks until the condition is open.

synchronized boolean

Closes the condition.

synchronized boolean

Returns whether the condition is opened.

synchronized boolean

Opens the condition and releases all threads that are blocked.

Public fields

isOpen

public boolean isOpen

Public constructors

ConditionVariable

public ConditionVariable()

Creates a closed instance using DEFAULT.

ConditionVariable

public ConditionVariable(Clock clock)

Creates an instance, which starts closed.

Parameters
Clock clock

The Clock whose elapsedRealtime method is used to determine when block should time out.

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 timeoutMs <= 0 then the call will return immediately without blocking.

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.

isOpen

synchronized public boolean isOpen()

Returns whether the condition is opened.

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.