ExecutorService(特别是ThreadPoolExecutor)线程安全吗? [英] Is ExecutorService (specifically ThreadPoolExecutor) thread safe?
问题描述
ExecutorService
是否保证线程安全?
Does the ExecutorService
guarantee thread safety ?
我将从不同的线程向同一个ThreadPoolExecutor提交作业,在交互/提交任务之前是否必须同步对执行程序的访问?
I'll be submitting jobs from different threads to the same ThreadPoolExecutor, do I have to synchronize access to the executor before interacting/submitting tasks?
推荐答案
这是真的,有问题的JDK类似乎没有明确保证线程安全的任务提交。但是,实际上,库中的所有ExecutorService实现都是以这种方式确实是线程安全的。我认为依靠这个是合理的。由于实现这些功能的所有代码都放在公共领域,因此绝对没有动机让任何人以不同的方式完全重写它。
It's true, the JDK classes in question don't seem to make an explicit guarantee of thread-safe task submission. However, in practice, all ExecutorService implementations in the library are indeed thread-safe in this way. I think it's reasonable to depend on this. Since all the code implementing these features was placed in the public domain, there's absolutely no motivation for anyone to completely rewrite it a different way.
这篇关于ExecutorService(特别是ThreadPoolExecutor)线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!