0

I have got an executor that executes a task every 5 sec

public class ScheduledTaskExecutor {

  public int execute(){
    ScheduledExecutorService executor = Executors.newScheduledThreadPool(4);
    executor.scheduleAtFixedRate(new Task().run,3,5, TimeUnit.SECONDS);
    return -1;
  }

}

Here's the task. I'm throwing an IllegalArgumentException if X == 4

public class Task {

    private static final Logger LOG = LoggerFactory.getLogger(DcEmailTask.class);
    private int x = 0;


    public Runnable run = () -> {
        String currentThread = Thread.currentThread().getName();
        x++;
        System.out.println("Thread [" + currentThread + "] is executing the task: " + x);
        if (x == 4) throw new IllegalArgumentException();
    };

}

The program stops executing and no stack trace is printed.

theyCallMeJun
  • 911
  • 1
  • 10
  • 21
  • And the question is ? Don't see anyone in the post – azro Jan 26 '19 at 09:50
  • @azro are you out here counting views and doing census?? – theyCallMeJun Jan 26 '19 at 10:25
  • I was just wondering what you wanted to do, afte read your post I did not found out, because in the title there is no question and you talk about "subsequent task" And i did not get it after reading, so don't be rude ;) – azro Jan 26 '19 at 10:26

2 Answers2

0

This behavior is as per the javadoc of ScheduledExecutorService#scheduleAtFixedRate. One way to solve the issue is to catch all the exceptions in runnable.

This answer has more details https://stackoverflow.com/a/24902026/3002471

  • so true but one may think only the executing thread should be terminated and not all threads in the pool. Anyways..thanks. – theyCallMeJun Jan 26 '19 at 10:24
0
  1. Implement ScheduledExecutorService interface (SESWrrapper) that accepts another ScheduledExecutorService in its constructor.
  2. Implement Runnable interface (SafeRunnableWrapper) that accepts another Runnable in its constructor and capture exceptions inside of its run() method.
  3. Implement Callable interface (SafeCallableWrapper) as above.
  4. In SESWrrapper.(Runnable, long, TimeUnit) methods wrap Runnable with SafeRunnableWrapper and invoke nested ScheduledExecutorService.schedule().
jnr
  • 790
  • 1
  • 7
  • 9