I'm having a little problem understanding how ScheduledThreadPoolExecutor works (I'm using SCA so don't bother with the annotations inside the code). That's part of the code from my Scheduler class:
@AllowsPassByReference
public ScheduledFuture<?> schedule(Task task)
{
future=scheduler.scheduleAtFixedRate(task.getRunnableContent(), task.getDelay(), task.getPeriod(), TimeUnit.MILLISECONDS);
return future;
}
@AllowsPassByReference
public void deschedule(Task task)
{
scheduler.remove(task.getRunnableContent());
}
And that's part of the code from my Task class:
public void scheduleTask()
{
if(!running)
{
future=scheduler.schedule(this);
running=true;
}
}
public void descheduleTask()
{
if(running)
{
future.cancel(mayInterruptIfRunning);
scheduler.deschedule(this);
running=false;
}
}
Now here's the big deal! Everywhere I looked people used cancel on ScheduledFutures and then used shutdown method on the scheduler, but I don't want to stop the scheduler. Let me explain a little better: in this application periodic tasks must be scheduled, stopped and re-scheduled individually at any time, so I need a way to interrupt a single task once it started running without having shutdown the whole scheduler service. I hope you can understand what I'm trying to do, any advice? Thanks :)