33import static java .util .concurrent .TimeUnit .MILLISECONDS ;
44import static java .util .concurrent .TimeUnit .MINUTES ;
55
6+ import dev .enola .common .concurrent .Executors ;
7+
68import java .time .Duration ;
79import java .time .Instant ;
810import java .util .Map ;
1113import java .util .concurrent .Callable ;
1214import java .util .concurrent .ConcurrentHashMap ;
1315import java .util .concurrent .ExecutorService ;
14- import java .util .concurrent .Executors ;
1516import java .util .concurrent .Future ;
1617import java .util .concurrent .FutureTask ;
1718import java .util .concurrent .ScheduledExecutorService ;
@@ -29,15 +30,20 @@ public class TaskExecutor implements AutoCloseable {
2930 // can still find them later, with a separate eviction policy for that persistent store.
3031 private final Map <UUID , Task <?, ?>> tasks = new ConcurrentHashMap <>();
3132
32- private final ExecutorService executor = TaskExecutorServices .newVirtualThreadPerTaskExecutor ();
33-
34- // TODO Use LoggingScheduledExecutorService from Enola Commons, for both.
33+ // Nota bene: In *THEORY* we should *NEVER* have *ANY* uncaught exceptions from Task,
34+ // because any exception thrown by the task's `execute()` method would be caught and
35+ // wrapped in an ExecutionException by the Future returned by ExecutorService.submit().
36+ //
37+ // But in practice, who knows what the future holds, so we better log them just in case;
38+ // just because "swallowed" lost exceptions are seriously the worst kind of bugs to diagnose!
39+ private final ExecutorService executor =
40+ Executors .newVirtualThreadPerTaskExecutorWithLoggingThreadUncaughtExceptionHandler (LOG );
3541
3642 private final ScheduledExecutorService timeoutScheduler =
37- Executors .newSingleThreadScheduledExecutor ();
43+ Executors .newSingleThreadScheduledExecutor ("TaskExecutor-Timeout" , LOG );
3844
3945 private final ScheduledExecutorService cleanupScheduler =
40- Executors .newSingleThreadScheduledExecutor ();
46+ Executors .newSingleThreadScheduledExecutor ("TaskExecutor-Cleanup" , LOG );
4147
4248 public TaskExecutor (Duration completedTaskEvictionInterval ) {
4349 var m = completedTaskEvictionInterval .toMinutes ();
0 commit comments