Package com.iizix.timeout.engine
Class TimeoutTask
java.lang.Object
com.iizix.timeout.engine.TimeoutTask
- All Implemented Interfaces:
- ITimeoutTask,- ITimerTask
The implementation of the interface for a registered timeout task that can be queried for timeout or canceled state.
- Author:
- Christopher Mindus
- Method SummaryModifier and TypeMethodDescription- boolean- cancel()Cancels the timeout task.- longGets the remaining time before timeout.- longGets the starting time.- booleanReturns whether timeout notification has been called or not.- booleanVerifies if timeout has occurred.- booleanChecks if the task is canceled.- longReturns the scheduled execution time of the most recent actual execution of this task.
- Method Details- scheduledExecutionTimepublic long scheduledExecutionTime()Returns the scheduled execution time of the most recent actual execution of this task. (If this method is invoked while task execution is in progress, the return value is the scheduled execution time of the ongoing task execution.)- This method is typically invoked from within a task's run method, to determine whether the current execution of the task is sufficiently timely to warrant performing the scheduled activity: 
 This method is typically not used in conjunction with fixed-delay execution repeating tasks, as their scheduled execution times are allowed to drift over time, and so are not terribly significant.- public void run() { if (System.currentTimeMillis() - scheduledExecutionTime() >= MAX_TARDINESS) return; // Too late; skip this execution. // Perform the task }- Returns:
- the time at which the most recent execution of this task was scheduled to occur, in the format returned by Date.getTime(). The return value is undefined if the task has yet to commence its first execution.
- See Also:
 
- isCanceledpublic boolean isCanceled()Checks if the task is canceled.- Specified by:
- isCanceledin interface- ITimeoutTask
- Returns:
- The canceled flag.
 
- getStartTimepublic long getStartTime()Gets the starting time.- Returns the starting time in milliseconds. Note that while the unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds. - See the description of the class - Datefor a discussion of slight discrepancies that may arise between "computer time" and coordinated universal time (UTC).- Specified by:
- getStartTimein interface- ITimerTask
- Returns:
- The difference, measured in milliseconds, between the start time and midnight, January 1, 1970 UTC.
- See Also:
 
- getRemainingTimepublic long getRemainingTime()Gets the remaining time before timeout. This value just keeps on counting, even when the timeout has occurred or the task is stopped.- Specified by:
- getRemainingTimein interface- ITimeoutTask
- Returns:
- A time in milliseconds >= zero indicating the remaining timeout time, or < zero if the task has timed out. The return value is Long.MAX_VALUEif the timeout is indefinite.
 
- hasTimedOutpublic boolean hasTimedOut()Verifies if timeout has occurred.- Specified by:
- hasTimedOutin interface- ITimeoutTask
- Returns:
- true if the task has timed out, false otherwise.
 
- hasNotifiedTimeoutpublic boolean hasNotifiedTimeout()Returns whether timeout notification has been called or not.- Specified by:
- hasNotifiedTimeoutin interface- ITimeoutTask
- Returns:
- true if the timeout task has been notified, false otherwise (e.g. task canceled).
 
- cancelpublic boolean cancel()Cancels the timeout task. This call can be done from any thread and will stop any timeout notification of- ITimeoutListener.onTimeout().- Specified by:
- cancelin interface- ITimerTask
- Returns:
- true for success, false if already canceled.