shutdown和awaitTermination哪个第一次调用

shutdown和awaitTermination哪个第一次调用

本文介绍了shutdown和awaitTermination哪个第一次调用有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两者之间有什么区别

ExecutorService eService = Executors.newFixedThreadPool(2);
eService.execute(new TestThread6());
eService.execute(new TestThread6());
eService.execute(new TestThread6());
eService.awaitTermination(1, TimeUnit.NANOSECONDS);
eService.shutdown();

eService.shutdown();
eService.awaitTermination(1, TimeUnit.NANOSECONDS);

我不太了解shutdown().此方法不等待先前提交的任务完成执行.这是否意味着shutdown()可以终止已提交但尚未完成的任务?我尝试了一些例子,他们没有证明,请给我一个例子.

I don't really understand shutdown(). This method does not wait for previously submitted tasks to complete execution. Does it mean shutdown() may terminate the tasks which have been submitted, but not completed? I tried some examples, they do not prove it, please give me an example.

推荐答案

您应先致电shutdown.否则,您可能要等待很长时间,因为awaitTermination实际上并没有关闭执行程序.

You should call shutdown first. Otherwise, you might be waiting for a very long time, since awaitTermination doesn't actually shut down your executor.

如果要等待任务完成而不是等待执行程序关闭,则应使用invokeAll.

If you wanted to wait for tasks to complete, rather than wait for the executor to shut down, then you should use invokeAll.

这篇关于shutdown和awaitTermination哪个第一次调用有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 06:50