public void testDelayedTasksReusePooledThreadIfExecuteAtDifferentTimes() throws InterruptedException, ExecutionException { final AppScheduledExecutorService service = new AppScheduledExecutorService(getName()); final List<LogInfo> log = Collections.synchronizedList(new ArrayList<>()); // pre-start one thread Future<?> future = service.submit(EmptyRunnable.getInstance()); future.get(); service.setBackendPoolCorePoolSize(1); assertEquals(1, service.getBackendPoolExecutorSize()); int delay = 500; ScheduledFuture<?> f1 = service.schedule((Runnable) () -> log.add(new LogInfo(1)), delay, TimeUnit.MILLISECONDS); ScheduledFuture<?> f2 = service.schedule( (Runnable) () -> log.add(new LogInfo(2)), delay + 100, TimeUnit.MILLISECONDS); ScheduledFuture<?> f3 = service.schedule( (Runnable) () -> log.add(new LogInfo(3)), delay + 200, TimeUnit.MILLISECONDS); assertEquals(1, service.getBackendPoolExecutorSize()); assertFalse(f1.isDone()); assertFalse(f2.isDone()); assertFalse(f3.isDone()); TimeoutUtil.sleep(delay + 200 + 300); assertTrue(f1.isDone()); assertTrue(f2.isDone()); assertTrue(f3.isDone()); assertEquals(1, service.getBackendPoolExecutorSize()); assertEquals(3, log.size()); Set<Thread> usedThreads = new HashSet<>( Arrays.asList( log.get(0).currentThread, log.get(1).currentThread, log.get(2).currentThread)); if (usedThreads.size() != 1) { System.err.println(ThreadDumper.dumpThreadsToString()); } assertEquals(usedThreads.toString(), 1, usedThreads.size()); // must be executed in same thread service.shutdownAppScheduledExecutorService(); assertTrue(service.awaitTermination(10, TimeUnit.SECONDS)); }