Class RepeatingTask
- All Implemented Interfaces:
Runnable
ScheduledExecutorService and this task controls if it is needed
to be rescheduled.
Note that as of Java 5 ScheduledThreadPoolExecutor is
preferred over Timer (because of its caveats). Most tasks
can be relatively easily ported to use the more robust
ScheduledThreadPoolExecutor. However there are certain tasks which
can be easily done using a Timer but not a
ScheduledExecutorService. One such particular task is when the task
itself needs to make sure that it never runs again. This is not so easy to
do with a ScheduledExecutorService because a task submitted to it
can be canceled by the future returned by the schedule method. This
class is intended to fill this gap.
To use this class subclass this abstract class and implement the
runAndTest() method. This method returns a
boolean to determine if the task can be scheduled again to run.
Thread safety
The methods of this class are safe to use by multiple threads concurrently but therunAndTest() method is not required to be thread-safe.
Synchronization transparency
The methods of this class are not synchronization transparent and therunAndTest() method is not required to be
synchronization transparent either.-
Constructor Summary
ConstructorsConstructorDescriptionRepeatingTask(ScheduledExecutorService executor, long period, TimeUnit periodUnit) Initializes aRepeatingTaskwith aScheduledExecutorServiceand the time to wait between consecutive execution of this task.RepeatingTask(ScheduledExecutorService executor, long period, TimeUnit periodUnit, boolean scheduleOnFailure) Initializes aRepeatingTaskwith aScheduledExecutorService, the time to wait between consecutive execution of this task and if it need to be rescheduled in case this task throws an unchecked exception. -
Method Summary
Modifier and TypeMethodDescriptionfinal voidexecute()Submits this task to be executed a single time as soon as possible by theScheduledExecutorServicespecified at construction time.final voidrun()Invokes therunAndTest()method and reschedules it to theScheduledExecutorServicespecified at construction time according to the construction time definitions.protected abstract booleanImplement this method to actually execute the given task.final voidSubmits this task to be executed a periodically after the given initial delay by theScheduledExecutorServicespecified at construction time.
-
Constructor Details
-
RepeatingTask
Initializes aRepeatingTaskwith aScheduledExecutorServiceand the time to wait between consecutive execution of this task. The task will be be rescheduled to execute again even if it throws an exception.To actually start execution this task periodically: Call the
schedule(long, TimeUnit)method or call theexecute()method to submit this task for execution for a single run (in this case it will be executed without delay).- Parameters:
executor- theScheduledExecutorServiceto which this task will be submitted to. This argument cannot benull.period- the time to wait between consecutive execution of this task in the given time unit. This argument must be greater than or equal to zero.periodUnit- the time unit of theperiodargument. This argument cannot benull.- Throws:
IllegalArgumentException- thrown ifperiod < 0NullPointerException- thrown if eitherexecutororperiodUnitisnull
-
RepeatingTask
public RepeatingTask(ScheduledExecutorService executor, long period, TimeUnit periodUnit, boolean scheduleOnFailure) Initializes aRepeatingTaskwith aScheduledExecutorService, the time to wait between consecutive execution of this task and if it need to be rescheduled in case this task throws an unchecked exception.To actually start execution this task periodically: Call the
schedule(long, TimeUnit)method or call theexecute()method to submit this task for execution for a single run (in this case it will be executed without delay).- Parameters:
executor- theScheduledExecutorServiceto which this task will be submitted to. This argument cannot benull.period- the time to wait between consecutive execution of this task in the given time unit. This argument must be greater than or equal to zero.periodUnit- the time unit of theperiodargument. This argument cannot benull.scheduleOnFailure-trueif the task needs to be rescheduled in case it throws an unchecked exception,falseif the task must not be executed again in case of such an exception- Throws:
IllegalArgumentException- thrown ifperiod < 0NullPointerException- thrown if eitherexecutororperiodUnitisnull
-
-
Method Details
-
execute
public final void execute()Submits this task to be executed a single time as soon as possible by theScheduledExecutorServicespecified at construction time.- See Also:
-
schedule
Submits this task to be executed a periodically after the given initial delay by theScheduledExecutorServicespecified at construction time.- Parameters:
delay- the initial delay before the first execution of this task in the given time unit. This argument must be greater than or equal to zero.delayUnit- the time unit of thedelayargument. This argument cannot benull.- Throws:
IllegalArgumentException- thrown ifdelay < 0NullPointerException- thrown if the specified time unit isnull- See Also:
-
runAndTest
protected abstract boolean runAndTest()Implement this method to actually execute the given task. This method will be invoked by therun()method of this task which will also reschedule this task if needed.- Returns:
trueif this task needed to be executed again,falsein case this task must not be executed again
-
run
public final void run()Invokes therunAndTest()method and reschedules it to theScheduledExecutorServicespecified at construction time according to the construction time definitions.Note that this method is only intended to be called by the underlying
ScheduledExecutorService.
-