I am using ScheduledExecutorService which uses ThreadFactory to create new threads. However when some exception occurs in the scheduled task, the UncaughtExceptionHandler in the threadfactory is not executed. Why is it happening?
Thread factory is as follows:
public class CustomThreadFactory {
public static ThreadFactory defaultFactory(String namePrefix) {
return new ThreadFactoryBuilder()
.setNameFormat(String.format("%s-%%d", namePrefix))
.setDaemon(false)
.setUncaughtExceptionHandler(new CustomExceptionHandler())
.build();
}
}
The Exception handler:
public class CustomExceptionHandler implements Thread.UncaughtExceptionHandler {
@Override
public void uncaughtException(Thread thread, Throwable t) {
log.error("Received uncaught exception {}", thread.getName(), t);
}
}
The main function:
public static void main(String[] args) {
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(
CustomThreadFactory.defaultFactory("scheduler"));
ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 20L,
TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(1));
scheduler.scheduleWithFixedDelay(
() -> {
executor.submit(() -> Thread.sleep(10000));
},0,1,TimeUnit.SECONDS);
}
I am stating my exact issue so that it does not end up with being XY problem. In the above snippet, the blocking queue has size one and tasks are added to blocking queue every second. So on adding third task, the blocking queue executor gives RejectionExecutionException
which is not printed by UncaughtExceptionHandler
.