18

我正在做一个项目,我需要确保每个线程都在特定范围内工作。例如:

NO_OF_THREADS: 2
NO_OF_TASKS: 10

如果number of threads is 2然后number of tasks is 10每个线程将执行10 tasks。所以这意味着 2 线程将做20 tasks

在实际情况下,这些数字(任务数和线程数)将非常高,因为它们都可以在我的代码中进行配置。

在上面的例子中,first thread应该使用 id between1 and 10并且second thread应该使用 id between11 and 20等等,如果有更多的线程。之后,每个线程将建立数据库连接,然后插入数据库。

所以我有我的下面的代码,它工作正常。

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. 我无法决定我应该选择哪些因素newCachedThreadPoolnewFixedThreadPool。所以这就是我发布我的场景的原因,我将用我的代码做什么。

谁能帮我看看我应该在这里选择什么?请详细解释一下为什么我们会根据哪些因素选择它,以便我可以很好地理解这一点。我已经浏览了 java 文档,但无法决定我应该在这里选择什么。

谢谢您的帮助。

4

2 回答 2

25

所以现在我想知道的是 - 我应该在我的代码中使用 newFixedThreadPool 还是 newCachedThreadPool ?

引用 Javadocs 的话newFixedThreadPool()

创建一个重复使用固定数量线程的线程池...

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.

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.

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.

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.

于 2013-02-25T01:58:19.930 回答
0

I feel like it is highly dependent on the type of job, If you have a function which is I/O bound, use newCachedThreadPool() if its a cpu intensive operation without I/O , use the other one.

于 2021-05-16T20:41:49.707 回答