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

[397 byte] By [BigBadBurrowa] at [2007-11-26 18:34:34]
# 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.

davidholmesa at 2007-7-9 6:08:40 > top of Java-index,Core,Core APIs...
# 2
I'm not sure if that would work, I don't know what size the pool will be prior to starting the ExecutorService
BigBadBurrowa at 2007-7-9 6:08:40 > top of Java-index,Core,Core APIs...
# 3
instead of a Cache pool you can use a fixed thread pool size. http://java.sun.com/javase/6/docs/api/java/util/concurrent/Executors.html#newFixedThreadPool(int)
yidalia at 2007-7-9 6:08:40 > top of Java-index,Core,Core APIs...
# 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.

davidholmesa at 2007-7-9 6:08:40 > top of Java-index,Core,Core APIs...
# 5
I gave up on it and just implemented it myself with a Vector and spin lock, same result and easier to implement : - )
BigBadBurrowa at 2007-7-9 6:08:40 > top of Java-index,Core,Core APIs...