如何决定使用newCachedThreadPool还是newFixedThreadPool? [英] How to decide whether to use newCachedThreadPool or newFixedThreadPool?
问题描述
我正在一个项目中,我需要确保每个线程都在特定范围内工作.例如:
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
如果number of threads is 2
和number of tasks is 10
,则每个线程将执行10 tasks
.因此,这意味着2个线程将执行20 tasks
.
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
.
在实际情况下,这些数字(任务数和线程数)将非常高,因为它们都可以在我的代码中进行配置.
在上面的示例中,first thread
应该在1 and 10
和second thread
之间使用id,在更多线程中,依此类推.然后,每个线程将建立数据库连接,然后插入数据库.
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.
所以我有下面的代码可以正常工作.
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
}
}
}
我的问题:-
我正在浏览互联网上的各种文章,并且读到有关newCachedThreadPool
的文章.所以现在我想知道的是-我应该在代码中使用newFixedThreadPool
还是newCachedThreadPool
吗?目前,我正在使用nexFixedThreadPool
.我无法决定应该选择哪些因素newCachedThreadPool
或newFixedThreadPool
.因此,这就是我发布方案要处理我的代码的原因.
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.
有人可以帮助我在这里选择什么吗?并且请详细解释我为什么我们在什么因素上选择它,以便我能很好地理解这一点.我已经阅读过Java文档,但无法决定我在这里应该选择什么.
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.
感谢您的帮助.
推荐答案
所以现在我想知道的是-我应该在代码中使用newFixedThreadPool还是newCachedThreadPool吗?
So now I am wondering is - Should I use newFixedThreadPool or newCachedThreadPool in my code?
要引用Javadocs,请引用newFixedThreadPool()
:
To quote from the Javadocs, the newFixedThreadPool()
:
创建一个重用固定数量线程的线程池...
Creates a thread pool that reuses a fixed number of threads...
这意味着,如果您请求2个线程,它将启动2个线程,而从不启动3个线程.另一方面,newCachedThreadPool()
:
This means that if you ask for 2 threads, it will start 2 threads and never start 3. On the other hand, the newCachedThreadPool()
:
创建一个线程池,该线程池可根据需要创建新线程,但在可用时将重用以前构造的线程.
Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available.
在您的情况下,如果您只有2个线程要运行,则任何一个都可以正常工作,因为您只会向池中提交2个作业.但是,如果要一次提交所有20个作业,但一次只运行2个作业,则应使用newFixedThreadPool(2)
.如果使用缓存池,则20个作业中的每个作业都会启动一个线程,该线程将同时运行,这可能不是最佳选择,具体取决于您拥有的CPU数.
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.
通常,当我需要要立即产生的线程时,即使当前正在运行的所有线程都在忙,我也会使用newCachedThreadPool()
.我最近在生成计时器任务时使用了它.并发作业的数量无关紧要,因为我从来没有产生太多,但是我希望它们在被请求时运行,并且希望它们重新使用休眠线程.
Typically I use the newCachedThreadPool()
when I need the 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.
当我想限制在任一点运行的并发任务的数量以最大化性能而不淹没服务器时,我使用了newFixedThreadPool()
.例如,如果我要处理一个文件中的10万行,一次只能处理一行,我不希望每一行都启动一个新线程,但是我想要某种程度的并发性,因此我分配了(例如)10个固定线程来运行直到池耗尽为止.
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.
这篇关于如何决定使用newCachedThreadPool还是newFixedThreadPool?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!