Class Deadline

java.lang.Object
com.peterphi.std.threading.Deadline
All Implemented Interfaces:
Serializable, Comparable<Deadline>

public final class Deadline extends Object implements Comparable<Deadline>, Serializable
Represents a deadline (some point in the future - generally, a time by which some operation should be completed)
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Deadline
    A deadline which will never arrive - representing the year 292278994, on Sun Aug 17 at 07:12:55 UTC
  • Constructor Summary

    Constructors
    Constructor
    Description
    Deadline(long timestamp)
     
    Deadline(long timeout, TimeUnit unit)
    Constructs a deadline using a given period of time
    Deadline(Timeout timeout)
    Constructs a new deadline based on adding timeout to the current time
    If current time + timeout > Long.MAX_VALUE then Long.MAX_VALUE is used as the deadline
    Deadline(Calendar calendar)
     
    Deadline(Date date)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    int
     
    boolean
     
    Returns the time represented by this deadline as a Date
    long
    Determines the amount of time left (in milliseconds) until the deadline; if the deadline has been reached or passed this method returns 0
    long
    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
    Determines the amount of time leftuntil the deadline and returns it as a timeout
    int
     
    boolean
    Determines whether the deadline has expired yet
    boolean
    Determine whether the deadline is still valid (ie.
    static Deadline
    latest(Deadline... deadlines)
    Retrieve the deadline with the most time remaining until it expires
    <T> T
    resolveFuture(Future<T> future)
    Resolves a Future, automatically cancelling it in the event of a timeout / failure
    <T> T
    resolveFuture(Future<T> future, boolean autocancel)
    Resolves a Future, potentially automatically cancelling it in the event of a timeout / failure
    Sleeps the current thread until interrupted or until this deadline has expired.
    boolean
    sleep(boolean mayInterrupt)
    Sleeps the current thread until this deadline has expired; if mayInterrupt is true then an interrupt will cause this method to return true
    This is essentially a neater way of calling Thread.sleep
    static Deadline
    soonest(Deadline... deadlines)
    Retrieve the deadline with the least time remaining until it expires
     
    void
    waitFor(Object monitor, boolean ignoreInterrupts)
    Waits on the listed monitor until it returns or until this deadline has expired; if mayInterrupt is true then an interrupt will cause this method to return true

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • MAX_VALUE

      public static final Deadline MAX_VALUE
      A deadline which will never arrive - representing the year 292278994, on Sun Aug 17 at 07:12:55 UTC
  • Constructor Details

    • Deadline

      public Deadline(Timeout timeout)
      Constructs a new deadline based on adding timeout to the current time
      If current time + timeout > Long.MAX_VALUE then Long.MAX_VALUE is used as the deadline
      Parameters:
      timeout -
    • Deadline

      public Deadline(long timestamp)
      Parameters:
      timestamp -
    • Deadline

      public Deadline(long timeout, TimeUnit unit)
      Constructs a deadline using a given period of time
      Parameters:
      timeout -
      unit -
    • Deadline

      public Deadline(Calendar calendar)
    • Deadline

      public Deadline(Date date)
  • 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

      public Timeout 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

      public Date getDate()
      Returns the time represented by this deadline as a Date
      Returns:
    • getTimeLeft

      public long getTimeLeft(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 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

      public Deadline sleep()
      Sleeps the current thread until interrupted or until this deadline has expired. Returns this no 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; if mayInterrupt is 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

      public void waitFor(Object monitor, boolean ignoreInterrupts)
      Waits on the listed monitor until it returns or until this deadline has expired; if mayInterrupt is 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

      public <T> T resolveFuture(Future<T> future) throws RuntimeException
      Resolves a Future, automatically cancelling it in the event of a timeout / failure
      Type Parameters:
      T -
      Parameters:
      future -
      Returns:
      Throws:
      RuntimeException
    • resolveFuture

      public <T> T resolveFuture(Future<T> future, boolean autocancel) throws RuntimeException
      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

      public int compareTo(Deadline that)
      Specified by:
      compareTo in interface Comparable<Deadline>
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
      See Also:
    • soonest

      public static Deadline soonest(Deadline... deadlines)
      Retrieve the deadline with the least time remaining until it expires
      Parameters:
      deadlines -
      Returns:
    • latest

      public static Deadline latest(Deadline... deadlines)
      Retrieve the deadline with the most time remaining until it expires
      Parameters:
      deadlines -
      Returns: