How to enforce concurrency of increasing threadpool size?
Hello,
I'm experimenting with the Executors class which was added in 1.5 to help with concurrency.
What I would like to achieve is a scenario where I can set say a maximum number of 10 threads to be running concurrently, each thread can create other threads to add to the thread pool. I'm not sure which (if any) method would enable me to do this. Any ideas?
Many thanks
# 1
You don't create threads to add to thread pools, you configure a thread pool with a core size, maximum size and a specific kind of blocking queue. Those elements in combination determine when the pool itself will create its own threads (taking into account you can pre-start core threads and that idle threads can timeout*).
For example, Executors.newCachedThreadPool() creates a thread pool with a core size of zero, a maximum of MAX_VALUE and uses a synchronous queue. Such a pool creates a new thread for each task submitted, unless there is an idle thread waiting to take the task.
So given that, can you rephrase your question?
* In Java 5 core threads can't timeout when idle, but they can in Java 6.
# 4
> I'm not sure if that would work, I don't know what
> size the pool will be prior to starting the
> ExecutorService
That statement doesn't make a lot of sense. You don't start ExecutorService instances - there is no start() method. If you get a pool via one of the Executors factory methods then initially there are zero threads in the pool.