public final class Deadline extends Object implements Comparable<Deadline>
Many systems use timeouts, which are relative to the start of the operation. However, being
relative causes them to be poorly suited for managing higher-level tasks where there are many
components and sub-operations that may not know the time of the initial "start of the operation."
However, a timeout can be converted to a Deadline
at the start of the operation and then
passed to the various components unambiguously.
Modifier and Type | Class and Description |
---|---|
static class |
Deadline.Ticker
Time source representing nanoseconds since fixed but arbitrary point in time.
|
Modifier and Type | Method and Description |
---|---|
static Deadline |
after(long duration,
TimeUnit units)
Create a deadline that will expire at the specified offset based on the
system ticker . |
static Deadline |
after(long duration,
TimeUnit units,
Deadline.Ticker ticker)
Create a deadline that will expire at the specified offset based on the given
Deadline.Ticker . |
int |
compareTo(Deadline that) |
boolean |
equals(Object o) |
static Deadline.Ticker |
getSystemTicker()
Returns the ticker that's based on system clock.
|
int |
hashCode() |
boolean |
isBefore(Deadline other)
Is
this deadline before another. |
boolean |
isExpired()
Has this deadline expired
|
Deadline |
minimum(Deadline other)
Return the minimum deadline of
this or an other deadline. |
Deadline |
offset(long offset,
TimeUnit units)
Create a new deadline that is offset from
this . |
ScheduledFuture<?> |
runOnExpiration(Runnable task,
ScheduledExecutorService scheduler)
Schedule a task to be run when the deadline expires.
|
long |
timeRemaining(TimeUnit unit)
How much time is remaining in the specified time unit.
|
String |
toString() |
public static Deadline.Ticker getSystemTicker()
This is EXPERIMENTAL API and may subject to change. If you'd like it to be
stabilized or have any feedback, please
public static Deadline after(long duration, TimeUnit units)
system ticker
.
If the given offset is extraordinarily long, say 100 years, the actual deadline created might saturate.
duration
- A non-negative duration.units
- The time unit for the duration.public static Deadline after(long duration, TimeUnit units, Deadline.Ticker ticker)
Deadline.Ticker
.
If the given offset is extraordinarily long, say 100 years, the actual deadline created might saturate.
CAUTION: Only deadlines created with the same Deadline.Ticker
instance can
be compared by methods like minimum(io.grpc.Deadline)
, isBefore(io.grpc.Deadline)
and compareTo(io.grpc.Deadline)
. Custom
Tickers should only be used in tests where you fake out the clock. Always use the system ticker
in production, or serious errors may occur.
This is EXPERIMENTAL API and may subject to change. If you'd like it to be
stabilized or have any feedback, please
duration
- A non-negative duration.units
- The time unit for the duration.ticker
- Where this deadline refer the current timepublic boolean isExpired()
true
if it has, otherwise false
.public boolean isBefore(Deadline other)
this
deadline before another. Two deadlines must be created using the same Deadline.Ticker
.public Deadline minimum(Deadline other)
this
or an other deadline. They must be created using
the same Deadline.Ticker
.other
- deadline to compare with this
.public Deadline offset(long offset, TimeUnit units)
this
.
If the given offset is extraordinarily long, say 100 years, the actual deadline created might saturate.
public long timeRemaining(TimeUnit unit)
TimeUnit.convert(long, java.util.concurrent.TimeUnit)
. If there is no time remaining, the returned duration is how
long ago the deadline expired.public ScheduledFuture<?> runOnExpiration(Runnable task, ScheduledExecutorService scheduler)
Note if this deadline was created with a custom Deadline.Ticker
, the scheduler
's
underlying clock should be synchronized with that Ticker. Otherwise the task won't be run at
the expected point of time.
task
- to run on expirationscheduler
- used to execute the taskScheduledFuture
which can be used to cancel execution of the taskpublic int compareTo(Deadline that)
Both deadlines must be created with the same Deadline.Ticker
.
compareTo
in interface Comparable<Deadline>