java 如何决定是使用 newCachedThreadPool 还是 newFixedThreadPool?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15058978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to decide whether to use newCachedThreadPool or newFixedThreadPool?
提问by Gray
I am working on a project in which I need to make sure each thread is working on a particular range. For example:
我正在做一个项目,我需要确保每个线程都在特定范围内工作。例如:
NO_OF_THREADS: 2
NO_OF_TASKS: 10
If number of threads is 2
and number of tasks is 10
then each thread will be performing 10 tasks
. So that means 2 thread will be doing 20 tasks
.
如果number of threads is 2
和number of tasks is 10
则每个线程将被执行10 tasks
。所以这意味着 2 个线程将执行20 tasks
。
In actual scenario these numbers (number of tasks and number of threads) will be very high as both of them are configurable in my code.
在实际场景中,这些数字(任务数和线程数)将非常高,因为它们都可以在我的代码中进行配置。
In the above example, first thread
should be using id between 1 and 10
and second thread
should be using id between 11 and 20
and so on if any more threads. And after that each thread will make a database connection and then insert into database.
在上面的例子中,如果有更多线程,first thread
应该使用 id between1 and 10
和second thread
应该使用 id between11 and 20
等等。之后每个线程将建立一个数据库连接,然后插入数据库。
So I have my below code which is working fine.
所以我有我下面的代码,它工作正常。
public static void main(String[] args) {
final int noOfThreads = 2;
final int noOfTasks = 10;
//create thread pool with given size
ExecutorService service = Executors.newFixedThreadPool(noOfThreads);
// queue some tasks
for (int i = 0, int nextId = 1; i < noOfThreads; i++, nextId += noOfTasks) {
service.submit(new ThreadTask(nextId, noOfTasks));
}
}
class ThreadTask implements Runnable {
private final int id;
private int noOfTasks;
public ThreadTask(int nextId, int noOfTasks) {
this.id = nextId;
this.noOfTasks = noOfTasks;
}
public void run() {
//make a database connection
for (int i = id; i < id + noOfTasks; i++) {
//insert into database
}
}
}
My question:-
我的问题:-
I was going through various article on the internet and I read about newCachedThreadPool
. So now I am wondering is - Should I use newFixedThreadPool
or newCachedThreadPool
in my code? As currently I am using nexFixedThreadPool
. I am not able to decide on what factors should I choose newCachedThreadPool
or newFixedThreadPool
. So that is the reason I posted my scenario what I am going to do with my code.
我浏览了互联网上的各种文章,并阅读了有关newCachedThreadPool
. 所以现在我想知道是 - 我应该在我的代码中使用newFixedThreadPool
还是newCachedThreadPool
?目前我正在使用nexFixedThreadPool
. 我无法决定应该选择哪些因素newCachedThreadPool
或newFixedThreadPool
. 所以这就是我发布我将如何处理我的代码的场景的原因。
Can anyone help me out what should I be choosing here? And please explain me in details why we are choosing that on what factors so that I can understand this very well. I have already gone through java docs but not able to decide what should I choose here.
谁能帮我看看我应该在这里选择什么?请详细解释为什么我们会根据哪些因素选择它,以便我能够很好地理解这一点。我已经浏览了 java 文档,但无法决定我应该在这里选择什么。
Thanks for the help.
谢谢您的帮助。
回答by Gray
So now I am wondering is - Should I use newFixedThreadPool or newCachedThreadPool in my code?
所以现在我想知道 - 我应该在我的代码中使用 newFixedThreadPool 还是 newCachedThreadPool ?
To quote from the Javadocs, the newFixedThreadPool()
:
引用 Javadocs 中的内容newFixedThreadPool()
:
Creates a thread pool that reuses a fixed number of threads...
创建一个重用固定数量线程的线程池...
This means that if you ask for 2 threads, it will start 2 threads and never start 3. On the other hand, the newCachedThreadPool()
:
这意味着,如果您请求 2 个线程,它将启动 2 个线程并且永远不会启动 3 个。另一方面,newCachedThreadPool()
:
Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available.
创建一个线程池,根据需要创建新线程,但在可用时将重用先前构造的线程。
In your case, if you only have 2 thread to run, either will work fine since you will only be submitting 2 jobs to your pool. However, if you wanted to submit all 20 jobs at once but only have 2 jobs running at one time, you should use a newFixedThreadPool(2)
. If you used a cached pool then each of the 20 jobs will start a thread which will run at the same time which may not be optimal depending on how many CPUs you have.
在您的情况下,如果您只有 2 个线程要运行,则任一线程都可以正常工作,因为您只会向池中提交 2 个作业。但是,如果您想一次提交所有 20 个作业,但一次只运行 2 个作业,则应使用newFixedThreadPool(2)
. 如果您使用缓存池,那么 20 个作业中的每一个都将启动一个线程,该线程将同时运行,这可能不是最佳的,具体取决于您拥有的 CPU 数量。
Typically I use the newCachedThreadPool()
when I needthe thread to be spawned immediately, even if all of the threads currently running are busy. I recently used it when I was spawning timer tasks. The number of concurrent jobs are immaterial because I never spawn very many but I want them to run when they are requested and I want them to re-use dormant threads.
通常,newCachedThreadPool()
当我需要立即生成线程时,我会使用,即使当前运行的所有线程都很忙。我最近在生成计时器任务时使用了它。并发作业的数量并不重要,因为我从不产生很多,但我希望它们在被请求时运行,并且我希望它们重新使用休眠线程。
I used newFixedThreadPool()
when I want to limit the number of concurrent tasks running at any one point to maximize performance and not swamp my server. For example if I am processing 100k lines from a file, one line at a time, I don't want each line to start a new thread but I want some level of concurrency so I allocate (for example) 10 fixed threads to run the tasks until the pool is exhausted.
我用newFixedThreadPool()
的时候我想限制在任何一个点上运行,以最大限度地提高性能,而不是淹没我的服务器的并发任务数。例如,如果我正在处理一个文件中的 100k 行,一次一行,我不希望每一行都启动一个新线程,但我想要某种程度的并发性,所以我分配(例如)10 个固定线程来运行直到池耗尽为止。