Java8-Executors-No.01

mac2022-06-30  122

import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; public class Executors1 { public static void main(String[] args) { test1(3); // test1(7); } private static void test1(long seconds) { ExecutorService executor = Executors.newSingleThreadExecutor(); executor.submit(() -> { try { TimeUnit.SECONDS.sleep(seconds); String name = Thread.currentThread().getName(); System.out.println("task finished: " + name); } catch (InterruptedException e) { System.err.println("task interrupted"); } }); stop(executor); } static void stop(ExecutorService executor) { try { System.out.println("attempt to shutdown executor"); executor.shutdown(); executor.awaitTermination(5, TimeUnit.SECONDS); } catch (InterruptedException e) { System.err.println("termination interrupted"); } finally { if (!executor.isTerminated()) { System.err.println("killing non-finished tasks"); } executor.shutdownNow(); System.out.println("shutdown finished"); } } }

转载于:https://www.cnblogs.com/bilaisheng/p/10210907.html

相关资源:Azkaban报错-azkaban.executor.ExecutorManagerException: No active executors found
最新回复(0)