Concurrency and restarting tasks
Hey,
I have an interesting (to me at least!) puzzle.
I have a task that can take a long time that I need to run periodically. I=
want to be able to cancel the execution of that task. I've been looking a=
t the Scheduler and Future objects, but I haven't seen how to cleanly stop,=
wait for a couple of seconds and restart the task on schedule. And my tas=
k won't always take X seconds--sometimes it will generate exceptions or tak=
e longer or shorter or any number of other things.
Right now I have:
<code>
final Runnable beeper = new Runnable()
{
public void run()
{
System.out.println("beep -- doing long task");
//doing long task
System.out.println("finished doing long task");
}
};
ScheduledFuture<?> beeperHandle;
try {
beeperHandle = scheduler.scheduleAtFixedRate( beeper, 1, 2, SECONDS);
beeperHandle.get(3, SECONDS);
} catch (TimeoutException te) {
System.out.println("Canceled due to timeout");
} catch (InterruptedException | ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
</code>
There are plenty of examples that show the first steps--setting up the sche=
dule and canceling. Restarting apparently is not as common.
Any ideas would be fantastic.
Thank you,
Me