Java – Threads or ThreadPool? Fixed or Dynamic ThreadPool

javamultithreading

I have a java program which listens on a port for input. Based on Input, it calls a webservice and then returns a success/failure back to the client program.

I fork a thread for each client connection. The response back to the client which connects to the program has to be quick.

These are the choices I am considering

  1. use regular threads
  2. use ExecutorService with newFixedThreadPool
  3. use ExecutorService with newCachedThreadPool

The reason I am considering Pools is because my threads are shortlived – they just call a webservice, return result to the client and close the connection.

I don't think newFixedThreadPool would be the right thing because then connections would be waiting in queues to get a thread.

newCachedThreadPool would have been perfect except for one thing – threads die after a minute. In my case, I get bursts of connections – i.e. multiple connections and then there may be a lull for a few minutes and then again bursts. I think the threads in the CachedThreadPool would die and and would again have to be recreated – so in this case, it may work like #1 sometimes.

Ideally I would have loved to have newCachedThreadPool with a minimum – i.e. a setting which says number of threads would never go below say 20. So idle threads are killed but never allow to go below a minimum threshold.

Is there anything like this available? Or are there any better alternatives?

Best Answer

The methods in the Executors class are just convenience methods for common use cases. There are a lot more options available for creating thread pools.

To create the same thing that Executors.newCachedThreadPool() does but with a minimum of 20 threads (this is copied from the method in Executors, with the core thread size changed from 0 to 20).

        return new ThreadPoolExecutor(20, Integer.MAX_VALUE,
                                  60L, TimeUnit.SECONDS,
                                  new SynchronousQueue<Runnable>());
Related Topic