https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/concurrent/ExecutorService.html#shutdownNow()
There are no guarantees beyond best-effort attempts to stop processing actively executing tasks. For example, typical implementations will cancel via Thread.interrupt(), so any task that fails to respond to interrupts may nefer terminate.
Java 不能强制终止线程,杀死一个线程需要那个线程配合,检测 interrupt 标志,处理 InterruptedException 异常。
//让主线程停止阻塞立即返回, 尝试下面两个方案都不行
主线程中创建一个 CompletableFuture done ,此处完成它
CompletableFuture.anyOf(done, CompletableFuture.allOf(list.toArray)).join()
等待的条件,从 任务全部完成 变成 (任务全部完成 or i >= 2) |