How to decide whether to use newCachedThreadPool or newFixedThreadPool?

user1813228 picture user1813228 · Feb 25, 2013 · Viewed 13.7k times · Source

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.

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.

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.

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.

Thanks for the help.

Answer

Gray picture Gray · Feb 25, 2013

So now I am wondering is - Should I use newFixedThreadPool or newCachedThreadPool in my code?

To quote from the Javadocs, the 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():

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.