Package com.unboundid.util
Class WakeableSleeper
- java.lang.Object
-
- com.unboundid.util.WakeableSleeper
-
- All Implemented Interfaces:
java.io.Serializable
@ThreadSafety(level=MOSTLY_NOT_THREADSAFE) public final class WakeableSleeper extends java.lang.Object implements java.io.Serializable
This class provides a utility that can be used to sleep for a specified period of time in a manner that allows it to be woken up if necessary. A single instance of this class may only be used to allow one thread to sleep at any given time, so if multiple threads need to sleep at the same time then a separateWakeableSleeperinstance should be used for each.- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description WakeableSleeper()Creates a new instance of this wakeable sleeper.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanisShutDown()Returntrueif thisWakeableSleeperinstance has been shutdown via theshutDown()method andfalseotherwise.voidshutDown()Permanently shuts down thisWakeableSleeperinstance.booleansleep(long time)Attempts to sleep for the specified length of time in milliseconds, subject to the accuracy available within the JVM and underlying system.voidwakeup()Indicates that the sleeper should wake up if it is currently sleeping.
-
-
-
Constructor Detail
-
WakeableSleeper
public WakeableSleeper()
Creates a new instance of this wakeable sleeper.
-
-
Method Detail
-
isShutDown
public boolean isShutDown()
Returntrueif thisWakeableSleeperinstance has been shutdown via theshutDown()method andfalseotherwise.- Returns:
trueif thisWakeableSleeperinstance has been shutdown via theshutDown()method andfalseotherwise.
-
sleep
@ThreadSafety(level=NOT_THREADSAFE) public boolean sleep(long time)
Attempts to sleep for the specified length of time in milliseconds, subject to the accuracy available within the JVM and underlying system. It may wake up prematurely if the wakeup method is called, or if the thread is interrupted. IfshutDown()is called, then any active caller of this method will return immediately, and subsequent calls will return without sleeping.
This method must not be called on the sameWakeableSleeperinstance by multiple threads at the same time.- Parameters:
time- The length of time in milliseconds to sleep.- Returns:
trueif the sleep completed, orfalseif it was woken or interrupted prematurely.
-
shutDown
@ThreadSafety(level=COMPLETELY_THREADSAFE) public void shutDown()
Permanently shuts down thisWakeableSleeperinstance. If a thread is currently blocked in thesleepmethod, it will return immediately, and all subsequent calls to that method will return without sleeping. It is safe to call this method multiple times.
-
wakeup
@ThreadSafety(level=COMPLETELY_THREADSAFE) public void wakeup()
Indicates that the sleeper should wake up if it is currently sleeping. This method will not make any attempt to ensure that the thread had woken up before returning. If multiple threads attempt to wake up the sleeper at the same time, then it will have the same effect as a single wakeup request.
-
-