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 givenAbstractTaskQueueExecutor
to run tasks, and starts the background execution thread. -
Method Summary
Modifier and TypeMethodDescriptionboolean
clear
(long id) Deprecated.Since 2.9.1, because of poor efficiency.boolean
Cancels the given timer task.boolean
Cancels the given timer task.void
exit()
Equivalent to a call to: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.Schedules a task to be run every interval milliseconds.Schedules a task to be run every interval milliseconds.boolean
Returnstrue
if all queued tasks are marked as daemon usingTaskScheduler.TimerTask.daemon()
or if there are no queued tasks.void
setErrorHandler
(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
TaskScheduler
is started with a standardTaskQueueExecutor
with at least two threads.- Since:
- 2.1
-
TaskScheduler
Creates a TaskScheduler with the givenAbstractTaskQueueExecutor
to 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.TimerTask
instance 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.TimerTask
instance 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.TimerTask
instance 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.TimerTask
instance 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.TimerTask
to cancel- Returns:
true
if 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.TimerTask
to cancel- Returns:
true
if 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.TimerTask
to cancel- Returns:
true
if the task was found and successfully canceled- Since:
- 2.1
- See Also:
-
isAllDaemon
public boolean isAllDaemon()Returnstrue
if all queued tasks are marked as daemon usingTaskScheduler.TimerTask.daemon()
or if there are no queued tasks.- Returns:
true
if 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, ornull
to remove an existing error handler- Since:
- 2.6
- See Also:
-