Java executorService.awaitTermination()是否会阻止主线程并等待?
我已经进行了此测试代码:
ExecutorService executor = Executors.newFixedThreadPool(3);
executor.execute(new Runnable() {
@Override
public void run() {
try {
System.out.println("run begins");
Thread.sleep(1000);
System.out.println("run ends");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
try {
executor.awaitTermination(0L, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
executor.shutdown();
System.out.println("-----------");
我希望,由于可运行的可运行应睡1秒然后结束,然后执行者等待其终止和关闭,最后,system.out.println(“ ----------------------------------) - ”);
被执行。
但是真正的输出是:
-----------
run begins
run ends
执行顺序对我来说没有多大意义。似乎executorService.awaitxxx和shutdown()不会阻止主线程。 System.out.println(“ -----------”);
是第一个打印的代码行,然后是可运行的代码。
这是设计的行为吗?
如果是,那么我的问题是:如果等待XXXX方法没有阻止主线程,那么主线程如何知道executorService
完成?
I've this test code:
ExecutorService executor = Executors.newFixedThreadPool(3);
executor.execute(new Runnable() {
@Override
public void run() {
try {
System.out.println("run begins");
Thread.sleep(1000);
System.out.println("run ends");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
try {
executor.awaitTermination(0L, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
executor.shutdown();
System.out.println("-----------");
I expected that, as the Runnable should sleep 1s and then ends, then executor await its termination and shutdown, finally, System.out.println("-----------");
is executed.
But the real output is:
-----------
run begins
run ends
The execution sequence doesn't make much sense to me. Seems the ExecutorService.awaitXXX and shutdown() doesn't block the main thread. The System.out.println("-----------");
was the first code line to print, and then the Runnable code.
Is this the designed behavior?
If yes, then my question is: if awaitXXXX method doesn't block the main thread, how does the main thread know when ExecutorService
is done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
方法
等待()
确实 block 调用线程,但是您已经指定了0
秒的超时。引用 javadoc :如果您期望
等待()
将中断提交的任务,那么您的期望是错误的,那就不会了。此方法仅允许您指定等待期间并报告执行者是否完成的时间。方法
shutdown()
也将 not> not 中断先前提交的任何任务:如果您想尝试中断提交的任务,请使用
shutdownnow()
。注意:使用
shutdown()
是一种首选方法。Method
awaitTermination()
does block the calling thread, but you've specified the timeout of0
seconds. Quote from the javadoc:In case if you expected that
awaitTermination()
will interrupt submitted tasks, then your expectation is wrong, it wouldn't do that. This method only allows you to specify the period to wait and reports whether executor is done or not.Method
shutdown()
also will not interrupt any tasks that are previously submitted:If you wish to try to interrupt submitted tasks, use
shutdownNow()
.Note: usage of
shutdown()
is a preferred approach.来自
executorService#eekingtermination
的Javadocs:这意味着
等待termination
仅在关闭请求后才能使用,因此您需要调用shutdown()
等待()
之前。此外,AS @alexander ivanchenko 的另一个答案提到,设置了0L的超时将不等待任何东西,
> code>>
>
>等待终止
指示该线程是否通过返回值终止。如果您的主线程被另一个线程中断,则会抛出InterruptedException
。From the Javadocs of
ExecutorService#awaitTermination
:This means that
awaitTermination
will only work after a shutdown request so you need to callshutdown()
beforeawaitTermination()
.Furthermore, as the other answer by @Alexander Ivanchenko mentions, setting a timeout of 0L will not wait for anything and
awaitTermination
indicates whether the thread terminated in time via a return value. TheInterruptedException
is thrown if your main thread is interrupted by another thread.