1

I was debating whether there is any point in using an ExecutorService for a simple, dynamically growing list of threads of the same class, all of which are always active. I understand that Executors.newFixedThreadPool(int) provides a thread pool that can contain any number of threads but only the number specified in the method are active at any point in time. I do not have such a limitation, IOW if I add 6 threads, I want all 6 to be running concurrently.

Should I still use the Executors framework, if there are advantages to using it, or should I simply put my threads in an ArrayList?

amphibient
  • 29,770
  • 54
  • 146
  • 240
  • The whole point of Executors framework is to provide a better control over managing threads.If you think, there is a use case that you could reuse these thread in future context, then I would recommend using cachedThreadPool, otherwise simply No – Phani Jul 24 '13 at 19:16
  • they will all run until i tell them all to stop, it is not like any will go to a waiting state or need to be cached for later use. – amphibient Jul 24 '13 at 19:19
  • Its better to work with Core classes then. – Phani Jul 24 '13 at 19:20

1 Answers1

2

Should I still use the Executors framework, if there are advantages to using it, or should I simply put my threads in an ArrayList?

Lot of people fork and manage their own threads. However, the main reason why the ExecutorService classes are so heavily recommended is that they take care of the management of the threads and associated tasks and reduce the amount of code that you have to write, debug, maintain. They, obviously, provide a lot of other features like Callable support, completion services, etc. which you may not use. But leaving the management to these core classes, even for simple project, is a good thing in itself. I use them even for single threaded pools because I want the Runnable task queue features of the thread-pool instead of writing my own BlockingQueue or other mechanism.

if I add 6 threads, I want all 6 to be running concurrently.

Then you should use the Executors.newCachedThreadPool(). If you submit 6 jobs (instance of Runnable or Callable) to it then 6 threads will be started to handle them. If 3 of the jobs finish and other jobs are submitted then the 3 dormant threads will run the other jobs.

NOTE: if you are submitting 1000 jobs, you will start 1000 threads which is most likely not what you want to do.


For example, to write this yourself would take a lot more code:

// a pool of 10 threads
ExecutorService threadPool = Executors.newFixedThreadPool(10);
// submit a bunch of jobs to they 10 threads for processing
for (MyJob job : jobsToDo) {
   threadPool.submit(job);
}
// no more tasks are being submitted so shutdown the pool
threadPool.shutdown();
// wait for the jobs to finish
threadPool.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
Gray
  • 115,027
  • 24
  • 293
  • 354
  • my thread pool will not be fixed, i.e. it can take any number of workers – amphibient Jul 24 '13 at 19:28
  • 1
    Then you should use the `newCachedThreadPool()` @amphibient. I've added details about it in my answer. – Gray Jul 24 '13 at 19:30
  • You may still want to limit the number of threads somehow since there are some costs involved, like stack size or scheduling overhead. – Ralf H Jul 24 '13 at 20:43
  • one more question: does the Executors API provide a functionality to easily kill each thread spawned in the ThreadPool or do i have to build in my own flag/semaphore to tell each thread from the master to stop running? – amphibient Jul 24 '13 at 21:44
  • The threads will terminate when there are no more tasks to run and the thread pool is shutdown @amphibient. In terms of the cached threads, if they have not been used in a while they will be terminated. See the javadocs for `newCachedThreadPool()`. – Gray Jul 24 '13 at 21:47
  • no, i meant is there a way to quickly kill all the threads, assuming they run in an infinite loop? – amphibient Jul 24 '13 at 22:01
  • You can call `threadPool.shutdownNow()` which cancels all of the jobs and interrupt all of the threads @amphibient. – Gray Jul 24 '13 at 22:05
  • didn't work. threads kept running after calling `threadPool.shutdownNow()` – amphibient Jul 24 '13 at 22:24
  • It sends them an interrupt @amphibient. Read the docs about that: http://stackoverflow.com/questions/9791361/thread-not-interrupting/9791479#9791479 – Gray Jul 24 '13 at 22:28