I have a task within a ScheduledExecutor
running on a regular interval. If there are particular exceptions which I want to boil up to the main thread, what is a clean way to do this? There doesn't seem to be a way to catch exceptions from a ScheduledFuture
, and if I simply attempt to throw a RuntimeException
on a scheduled task, that task will cease and never be run again by the ScheduledExecutor
.
Currently, I just have a public static member in the main thread that I use to receive events from scheduled tasks but this hardly seems ideal. Any recommended reading for me to look at or any ideas?
Edit: Apologies, allow me to expand on this with an example:
public class TestScheduledFuture {
public static void main(String[] args) {
ScheduledExecutorService taskScheduler = Executors.newSingleThreadScheduledExecutor();
ScheduledFuture<?> scheduledFuture = taskScheduler.scheduleAtFixedRate(new TestScheduledTask(),2,2, TimeUnit.SECONDS);
}
}
My goal here is to simply throw a RuntimeException() in main() if I ever encounter a RuntimeException within TestScheduledTask().
Right now, I have a public static concurrentQueue in the main class which I add exceptions into from the scheduled threads. Perioidically in main() I simply poll the queue and react to any exceptions in the queue. Is there a better way to do this?