Class Timeout

java.lang.Object
org.aoju.bus.core.io.timout.Timeout
Direct Known Subclasses:
AssignTimeout, AsyncTimeout

public class Timeout extends Object
在放弃一项任务之前要花多少时间的策略 当一个任务 超时时,它处于未指定的状态,应该被放弃 例如,如果从源读取超时,则应关闭该源并 稍后应重试读取 如果向接收器写入超时,也是一样 适用规则:关闭洗涤槽,稍后重试
Since:
Java 17+
Author:
Kimi Liu
  • Field Details

    • NONE

      public static final Timeout NONE
      既不跟踪也不检测超时的空超时。在不需要超时 的情况下使用它,例如在操作不会阻塞的实现中.
  • Constructor Details

    • Timeout

      public Timeout()
  • Method Details

    • timeout

      public Timeout timeout(long timeout, TimeUnit unit)
      Parameters:
      timeout - long
      unit - TimeUnit
      Returns:
      timeout

      Wait at most timeout time before aborting an operation. Using a per-operation timeout means that as long as forward progress is being made, no sequence of operations will fail.

      If timeout == 0, operations will run indefinitely. (Operating system timeouts may still apply.)

    • timeoutNanos

      public long timeoutNanos()
      Returns:
      the timeout in nanoseconds, or 0 for no timeout.
    • hasDeadline

      public boolean hasDeadline()
      Returns:
      hasDeadline true if a deadline is enabled.
    • deadlineNanoTime

      public long deadlineNanoTime()
      Returns:
      deadlineNanoTime Returns the nano time when the deadline will be reached.
      Throws:
      IllegalStateException - if no deadline is set.
    • deadlineNanoTime

      public Timeout deadlineNanoTime(long deadlineNanoTime)
      Parameters:
      deadlineNanoTime - long
      Returns:
      timeout Sets the nano time when the deadline will be reached. All operations must complete before this time. Use a deadline to set a maximum bound on the time spent on a sequence of operations.
    • deadline

      public final Timeout deadline(long duration, TimeUnit unit)
      Parameters:
      duration - long
      unit - TimeUnit
      Returns:
      timeout Set a deadline of now plus duration time.
    • clearTimeout

      public Timeout clearTimeout()
      Returns:
      this Clears the timeout. Operating system timeouts may still apply.
    • clearDeadline

      public Timeout clearDeadline()
      Returns:
      this Clears the deadline.
    • throwIfReached

      public void throwIfReached() throws IOException
      Throws an InterruptedIOException if the deadline has been reached or if the current thread has been interrupted. This method doesn't detect timeouts; that should be implemented to asynchronously abort an in-progress operation.
      Throws:
      IOException - 抛出异常
    • waitUntilNotified

      public final void waitUntilNotified(Object monitor) throws InterruptedIOException
      Parameters:
      monitor - Waits on monitor until it is notified. Throws InterruptedIOException if either the thread is interrupted or if this timeout elapses before monitor is notified. The caller must be synchronized on monitor.
      Throws:
      InterruptedIOException - 抛出异常