Class TaskScheduler
setTimeout and setInterval.
Tasks are run in a separate background thread. Multiple tasks may run concurrently. This implementation uses a TaskQueueExecutor.
Timing may be inaccurate, depending on the platform. The implementation uses Object.wait(long).
This class is thread-safe.
- Since:
- 2.1
-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsConstructorDescriptionCreates a TaskScheduler and starts the background execution thread.TaskScheduler(AbstractTaskQueueExecutor executor) Creates a TaskScheduler with the givenAbstractTaskQueueExecutorto run tasks, and starts the background execution thread. -
Method Summary
Modifier and TypeMethodDescriptionbooleanclear(long id) Deprecated.Since 2.9.1, because of poor efficiency.booleanCancels the given timer task.booleanCancels the given timer task.voidexit()Equivalent to a call to:voidexit(boolean blocking) Exits this TaskScheduler by running any remaining tasks (also waiting for ones that are to be run in the future and non-daemon) and exiting the worker threads.Schedules a task to be run every interval milliseconds.Schedules a task to be run every interval milliseconds.booleanReturnstrueif all queued tasks are marked as daemon usingTaskScheduler.TimerTask.daemon()or if there are no queued tasks.voidsetErrorHandler(Consumer<Throwable> errorHandler) Sets the error handler that will be called when an error occurs while queuing a task.Schedules a task to be run at the specified timeout relative to the time this function was called.Schedules a task to be run at the specified timeout relative to the time this function was called.
-
Constructor Details
-
TaskScheduler
public TaskScheduler()Creates a TaskScheduler and starts the background execution thread.The
TaskScheduleris started with a standardTaskQueueExecutorwith at least two threads.- Since:
- 2.1
-
TaskScheduler
Creates a TaskScheduler with the givenAbstractTaskQueueExecutorto run tasks, and starts the background execution thread.- Parameters:
executor- The executor to run tasks with- Since:
- 2.12.0
-
-
Method Details
-
timeout
Schedules a task to be run at the specified timeout relative to the time this function was called.- Parameters:
handler- The handler to be run at the specified timeouttimeout- The offset in milliseconds when the handler should be called- Returns:
- The
TaskScheduler.TimerTaskinstance of this task. May be used as an argument to a subsequent call toclear(TimerTask) - Since:
- 2.6
- See Also:
-
timeout
Schedules a task to be run at the specified timeout relative to the time this function was called.- Parameters:
handler- The handler to be run at the specified timeouttimeout- The offset in milliseconds when the handler should be calledargs- Arguments to be passed to the handler- Returns:
- The
TaskScheduler.TimerTaskinstance of this task. May be used as an argument to a subsequent call toclear(TimerTask) - Since:
- 2.1
- See Also:
-
interval
Schedules a task to be run every interval milliseconds. The task is first run in interval milliseconds relative to the time this function was called.- Parameters:
handler- The handler to be run at the specified intervalinterval- The time in milliseconds between calls- Returns:
- The
TaskScheduler.TimerTaskinstance of this task. May be used as an argument to a subsequent call toclear(TimerTask) - Since:
- 2.6
- See Also:
-
interval
Schedules a task to be run every interval milliseconds. The task is first run in interval milliseconds relative to the time this function was called.- Parameters:
handler- The handler to be run at the specified intervalinterval- The time in milliseconds between callsargs- Arguments to be passed to the handler- Returns:
- The
TaskScheduler.TimerTaskinstance of this task. May be used as an argument to a subsequent call toclear(TimerTask) - Since:
- 2.1
- See Also:
-
clear
Cancels the given timer task.- Parameters:
tt- TheTaskScheduler.TimerTaskto cancel- Returns:
trueif tt is notnull- Throws:
ClassCastException- If the given parameter is not aTimerTask- Since:
- 2.9.1
- See Also:
-
clear
Cancels the given timer task.- Parameters:
tt- TheTaskScheduler.TimerTaskto cancel- Returns:
trueif tt is notnull- Since:
- 2.1
- See Also:
-
clear
Deprecated.Since 2.9.1, because of poor efficiency. Useclear(TimerTask)orclear(Object)instead.Cancels the given timer task.- Parameters:
id- The id of theTaskScheduler.TimerTaskto cancel- Returns:
trueif the task was found and successfully canceled- Since:
- 2.1
- See Also:
-
isAllDaemon
public boolean isAllDaemon()Returnstrueif all queued tasks are marked as daemon usingTaskScheduler.TimerTask.daemon()or if there are no queued tasks.- Returns:
trueif all tasks are daemon tasks- Since:
- 2.1
-
exit
public void exit()Equivalent to a call to:exit(false)- Since:
- 2.1
-
exit
public void exit(boolean blocking) Exits this TaskScheduler by running any remaining tasks (also waiting for ones that are to be run in the future and non-daemon) and exiting the worker threads.If blocking is
true, the caller thread is blocked until all remaining non-daemon tasks are run and all worker threads have exited; otherwise, the shutdown procedure is run by a separate thread.- Parameters:
blocking- Whether the call to this method should block until the shutdown procedure is complete- Since:
- 2.1
-
setErrorHandler
Sets the error handler that will be called when an error occurs while queuing a task. Also sets the error handler of thisTaskScheduler'sTaskQueueExecutor.If an error occurs while queuing a task and no handler is set, the error is printed to
stderr. For default behavior when an error occurs while running a task, seeTaskQueueExecutor.setErrorHandler(Consumer).- Parameters:
errorHandler- The error handler, ornullto remove an existing error handler- Since:
- 2.6
- See Also:
-