Package com.peterphi.std.threading
Class Deadline
java.lang.Object
com.peterphi.std.threading.Deadline
- All Implemented Interfaces:
Serializable,Comparable<Deadline>
Represents a deadline (some point in the future - generally, a time by which some operation should be completed)
- See Also:
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionDeadline(long timestamp) Constructs a deadline using a given period of timeConstructs a new deadline based on addingtimeoutto the current time
Ifcurrent time + timeout > Long.MAX_VALUEthen Long.MAX_VALUE is used as the deadline -
Method Summary
Modifier and TypeMethodDescriptionintbooleangetDate()Returns the time represented by this deadline as a DatelongDetermines the amount of time left (in milliseconds) until the deadline; if the deadline has been reached or passed this method returns 0longgetTimeLeft(TimeUnit unit) Determines the amount of time left (in a custom unit) until the deadline; if the deadline has been reached or passed this method returns 0Determines the amount of time leftuntil the deadline and returns it as a timeoutinthashCode()booleanDetermines whether the deadline has expired yetbooleanisValid()Determine whether the deadline is still valid (ie.static DeadlineRetrieve the deadline with the most time remaining until it expires<T> TresolveFuture(Future<T> future) Resolves a Future, automatically cancelling it in the event of a timeout / failure<T> TresolveFuture(Future<T> future, boolean autocancel) Resolves a Future, potentially automatically cancelling it in the event of a timeout / failuresleep()Sleeps the current thread until interrupted or until this deadline has expired.booleansleep(boolean mayInterrupt) Sleeps the current thread until this deadline has expired; ifmayInterruptis true then an interrupt will cause this method to return true
This is essentially a neater way of calling Thread.sleepstatic DeadlineRetrieve the deadline with the least time remaining until it expirestoString()voidWaits on the listed monitor until it returns or until this deadline has expired; ifmayInterruptis true then an interrupt will cause this method to return true
-
Field Details
-
MAX_VALUE
A deadline which will never arrive - representing the year 292278994, on Sun Aug 17 at 07:12:55 UTC
-
-
Constructor Details
-
Deadline
Constructs a new deadline based on addingtimeoutto the current time
Ifcurrent time + timeout > Long.MAX_VALUEthen Long.MAX_VALUE is used as the deadline- Parameters:
timeout-
-
Deadline
public Deadline(long timestamp) - Parameters:
timestamp-
-
Deadline
Constructs a deadline using a given period of time- Parameters:
timeout-unit-
-
Deadline
-
Deadline
-
-
Method Details
-
isExpired
public boolean isExpired()Determines whether the deadline has expired yet- Returns:
-
isValid
public boolean isValid()Determine whether the deadline is still valid (ie. has not expired yet)- Returns:
-
getTimeoutLeft
Determines the amount of time leftuntil the deadline and returns it as a timeout- Returns:
- a timeout representing the amount of time remaining until this deadline expires
-
getTimeLeft
public long getTimeLeft()Determines the amount of time left (in milliseconds) until the deadline; if the deadline has been reached or passed this method returns 0- Returns:
- the number of milliseconds left until this deadline expires
-
getDate
Returns the time represented by this deadline as a Date- Returns:
-
getTimeLeft
Determines the amount of time left (in a custom unit) until the deadline; if the deadline has been reached or passed this method returns 0- Parameters:
unit- a unit of time- Returns:
- the amount of time left before this deadline expires, converted (using
TimeUnit.convert) into the given Time Unit
-
sleep
Sleeps the current thread until interrupted or until this deadline has expired. Returnsthisno matter what condition causes this method's termination
This is essentially a neater way of calling Thread.sleep- Returns:
- itself
-
sleep
public boolean sleep(boolean mayInterrupt) Sleeps the current thread until this deadline has expired; ifmayInterruptis true then an interrupt will cause this method to return true
This is essentially a neater way of calling Thread.sleep- Parameters:
mayInterrupt- whether this method may be interrupted by an InterruptedException- Returns:
- whether the sleep was ended early by an interrupt
-
waitFor
Waits on the listed monitor until it returns or until this deadline has expired; ifmayInterruptis true then an interrupt will cause this method to return true- Parameters:
ignoreInterrupts- false if the code should return early in the event of an interrupt, otherwise true if InterruptedExceptions should be ignored
-
resolveFuture
Resolves a Future, automatically cancelling it in the event of a timeout / failure- Type Parameters:
T-- Parameters:
future-- Returns:
- Throws:
RuntimeException
-
resolveFuture
Resolves a Future, potentially automatically cancelling it in the event of a timeout / failure- Type Parameters:
T- the return type- Parameters:
future-autocancel- if the thread should be cancelled if the deadline expires within this method- Returns:
- the resolved value of the future (or null in the event of a timeout)
- Throws:
RuntimeException- if the operation fails (or is cancelled by another party)
-
compareTo
- Specified by:
compareToin interfaceComparable<Deadline>
-
hashCode
public int hashCode() -
equals
-
toString
-
soonest
Retrieve the deadline with the least time remaining until it expires- Parameters:
deadlines-- Returns:
-
latest
Retrieve the deadline with the most time remaining until it expires- Parameters:
deadlines-- Returns:
-