Class Deadline
- All Implemented Interfaces:
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Time source representing nanoseconds since fixed but arbitrary point in time. -
Method Summary
Modifier and TypeMethodDescriptionstatic Deadline
Create a deadline that will expire at the specified offset based on thesystem ticker
.static Deadline
after
(long duration, TimeUnit units, Deadline.Ticker ticker) Create a deadline that will expire at the specified offset based on the givenDeadline.Ticker
.int
boolean
static Deadline.Ticker
Returns the ticker that's based on system clock.int
hashCode()
boolean
Isthis
deadline before another.boolean
Returns whether this has deadline expired.Return the minimum deadline ofthis
or an other deadline.Create a new deadline that is offset fromthis
.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.toString()
-
Method Details
-
getSystemTicker
Returns the ticker that's based on system clock.This is EXPERIMENTAL API and may subject to change. If you'd like it to be stabilized or have any feedback, please let us know.
- Since:
- 1.24.0
-
after
Create a deadline that will expire at the specified offset based on thesystem ticker
.If the given offset is extraordinarily long, say 100 years, the actual deadline created might saturate.
- Parameters:
duration
- A non-negative duration.units
- The time unit for the duration.- Returns:
- A new deadline.
-
after
Create a deadline that will expire at the specified offset based on the givenDeadline.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 likeminimum(io.grpc.Deadline)
,isBefore(io.grpc.Deadline)
andcompareTo(io.grpc.Deadline)
. Custom Tickers should only be used in tests where you fake out the clock. Always use thesystem 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 let us know.
- Parameters:
duration
- A non-negative duration.units
- The time unit for the duration.ticker
- Where this deadline refer the current time- Returns:
- A new deadline.
- Since:
- 1.24.0
-
isExpired
public boolean isExpired()Returns whether this has deadline expired.- Returns:
true
if it has, otherwisefalse
.
-
isBefore
Isthis
deadline before another. Two deadlines must be created using the sameDeadline.Ticker
. -
minimum
Return the minimum deadline ofthis
or an other deadline. They must be created using the sameDeadline.Ticker
.- Parameters:
other
- deadline to compare withthis
.
-
offset
Create a new deadline that is offset fromthis
.If the given offset is extraordinarily long, say 100 years, the actual deadline created might saturate.
-
timeRemaining
How much time is remaining in the specified time unit. Internal units are maintained as nanoseconds and conversions are subject to the constraints documented forTimeUnit.convert(long, java.util.concurrent.TimeUnit)
. If there is no time remaining, the returned duration is how long ago the deadline expired. -
runOnExpiration
Schedule a task to be run when the deadline expires.Note if this deadline was created with a custom
Deadline.Ticker
, thescheduler
's underlying clock should be synchronized with that Ticker. Otherwise the task won't be run at the expected point of time.- Parameters:
task
- to run on expirationscheduler
- used to execute the task- Returns:
ScheduledFuture
which can be used to cancel execution of the task
-
toString
-
compareTo
Both deadlines must be created with the same
Deadline.Ticker
.- Specified by:
compareTo
in interfaceComparable<Deadline>
-
hashCode
public int hashCode() -
equals
-