public class ConditionHelper extends Object
Condition class built in the java.util.concurrent.locks package
without processing Lock and java.lang.InterruptedException exceptions.
The ConditionHelper object provides interfaces for suspending a thread (making it wait) or
waking up a waiting thread.
It supports infinite wait or timed wait. The thread will not exit the waiting state until it is awakened
or the specified waiting time elapses.
Pay attention to the following when using the ConditionHelper class:
ConditionHelper#await
or ConditionHelper#await(long awaitMs) method next time.ConditionHelper#await(long awaitMs) method does not ensure precise timing.java.lang.InterruptedException exception occurs during the waiting period,
the waiting state does not terminate. After the thread is awakened or the waiting time elapses,
you can use Thread.currentThread().isInterrupted() to check whether the thread is interrupted.| 构造器 | 说明 |
|---|---|
ConditionHelper() |
A constructor used to create a
ConditionHelper instance with the default wakeup state false. |
ConditionHelper(boolean isWakeup) |
A constructor used to create a
ConditionHelper instance with the specified wakeup state. |
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
void |
await() |
Suspends the current thread until it is awakened.
|
boolean |
await(long awaitMs) |
Suspends the current thread until it is awakened or the waiting time elapses.
|
void |
resetCondition() |
Resets the wakeup state back to
false, and then you can call await(). |
void |
wakeup() |
Wakes up the thread of the current object and sets its wakeup state to
true. |
public ConditionHelper()
ConditionHelper instance with the default wakeup state false.public ConditionHelper(boolean isWakeup)
ConditionHelper instance with the specified wakeup state.isWakeup - Indicates the wakeup state to set. The value true indicates that the
thread is awakened, and value false indicates that the thread is not awakened.public void wakeup()
true.
If you do not reset the wakeup state by calling resetCondition(), the execution result will
be returned immediately when you call the await() or await(long awaitMs) method next time.
public void resetCondition()
false, and then you can call await().public void await()
If the wakeup state of the object is true, the execution result of this method will be
returned immediately.
If a java.lang.InterruptedException exception occurs, the current thread continues to
wait until it is awakened. After it is awakened, you can use Thread.currentThread().isInterrupted()
to check whether the thread is interrupted.
public boolean await(long awaitMs)
If the wakeup state of the object is true, the execution result of this method
will be returned immediately.
If a java.lang.InterruptedException exception occurs, the current thread continues to wait.
After the thread is awakened or the waiting time elapses, you can use
Thread.currentThread().isInterrupted() to check whether the thread is interrupted.
Note that this method does not ensure precise timing because it uses the system clock. The timing precision may be affected if the system clock changes.
awaitMs - Indicates the waiting time, in milliseconds. If this parameter is set to 0,
the thread keeps waiting.true if the thread waits for being awakened;
returns false if the thread waits until the waiting time elapses.