@946898963
2022-05-25T20:55:31.000000Z
字数 1568
阅读 422
Java
public static ExecutorService executorService = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws InterruptedException {
executorService.submit(() -> System.out.println(Thread.currentThread().getName()));
long curTime = System.currentTimeMillis();
boolean b = executorService.awaitTermination(3, TimeUnit.SECONDS);
long lastTime = System.currentTimeMillis();
System.out.println("duration:" + (lastTime - curTime) + " result:" + b);
}
pool-1-thread-1
duration:3004 result:false
public static ExecutorService executorService = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws InterruptedException {
executorService.submit(() -> System.out.println(Thread.currentThread().getName()));
executorService.shutdown();
long curTime = System.currentTimeMillis();
boolean b = executorService.awaitTermination(3, TimeUnit.SECONDS);
long lastTime = System.currentTimeMillis();
System.out.println("duration:" + (lastTime - curTime) + " result:" + b);
}
pool-1-thread-1
duration:0 result:true
public static ExecutorService executorService = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws InterruptedException {
executorService.submit(() -> System.out.println(Thread.currentThread().getName()));
new Thread(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
executorService.shutdown();
}).start();
long curTime = System.currentTimeMillis();
boolean b = executorService.awaitTermination(3, TimeUnit.SECONDS);
long lastTime = System.currentTimeMillis();
System.out.println("duration:" + (lastTime - curTime) + " result:" + b);
}
pool-1-thread-1
duration:1004 result:true