Package org.aoju.bus.core.io.timout
Class Timeout
java.lang.Object
org.aoju.bus.core.io.timout.Timeout
- Direct Known Subclasses:
AssignTimeout,AsyncTimeout
在放弃一项任务之前要花多少时间的策略 当一个任务 超时时,它处于未指定的状态,应该被放弃 例如,如果从源读取超时,则应关闭该源并 稍后应重试读取 如果向接收器写入超时,也是一样 适用规则:关闭洗涤槽,稍后重试
- Since:
- Java 17+
- Author:
- Kimi Liu
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionClears the deadline.Clears the timeout.final TimeoutSet a deadline of now plusdurationtime.longReturns the nano time when the deadline will be reached.deadlineNanoTime(long deadlineNanoTime) Sets the nano time when the deadline will be reached.booleanReturns true if a deadline is enabled.voidThrows anInterruptedIOExceptionif the deadline has been reached or if the current thread has been interrupted.Wait at mosttimeouttime before aborting an operation.longReturns the timeout in nanoseconds, or0for no timeout.final voidwaitUntilNotified(Object monitor) Waits onmonitoruntil it is notified.
-
Field Details
-
NONE
既不跟踪也不检测超时的空超时。在不需要超时 的情况下使用它,例如在操作不会阻塞的实现中.
-
-
Constructor Details
-
Timeout
public Timeout()
-
-
Method Details
-
timeout
Wait at mosttimeouttime 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, or0for no timeout. -
hasDeadline
public boolean hasDeadline()Returns true if a deadline is enabled. -
deadlineNanoTime
public long deadlineNanoTime()Returns the nano time when the deadline will be reached.- Throws:
IllegalStateException- if no deadline is set.
-
deadlineNanoTime
-
deadline
-
clearTimeout
Clears the timeout. Operating system timeouts may still apply. -
clearDeadline
Clears the deadline. -
throwIfReached
Throws anInterruptedIOExceptionif 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
Waits onmonitoruntil it is notified. ThrowsInterruptedIOExceptionif either the thread is interrupted or if this timeout elapses beforemonitoris notified. The caller must be synchronized onmonitor.Here's a sample class that uses
waitUntilNotified()to await a specific state. Note that the call is made within a loop to avoid unnecessary waiting and to mitigate spurious notifications.class Dice { Random random = new Random(); int latestTotal; public synchronized void roll() { latestTotal = 2 + random.nextInt(6) + random.nextInt(6); System.out.println("Rolled " + latestTotal); notifyAll(); } public void rollAtFixedRate(int period, TimeUnit timeUnit) { Executors.newScheduledThreadPool(0).scheduleAtFixedRate(new Runnable() { public void run() { roll(); } }, 0, period, timeUnit); } public synchronized void awaitTotal(Timeout timeout, int total) throws InterruptedIOException { while (latestTotal != total) { timeout.waitUntilNotified(this); } } }- Throws:
InterruptedIOException
-