[concurrency-interest] PooledExecutor vs java.util.concurrent

Tim Peierls tim at peierls.net
Tue May 17 19:04:46 EDT 2005


Jonathan Baxter wrote:
>>>Used to be really easy to create a bounded, blocking thread pool:
>>>
>>>PooledExecutor pe = new PooledExecutor(10);
>>>pe.waitWhenBlocked();
>>>
>>>Was also easy to increase and decrease the maximum number of threads
>>>available, via setMaximumPoolSize and createThreads.
>>>
>>>How do you get the same behaviour under java.util.concurrent?
>>
>> [Tim Peierls]
>>  ExecutorService exec = new ThreadPoolExecutor(1, 10, 1, TimeUnit.MINUTES,
>>                                          new SynchronousQueue<Runnable>());
>>
>>Doesn't seem too hard. setMaximumPoolSize() is still there, and you can
>>prestartAllCoreThreads() instead of createThreads().
>
> [Jonathan Baxter]
> But does this block when the pool is full? Doesn't the 
> RejectedExecutionHandler get called instead? Seems that ThreadPoolExecutor 
> uses BlockingQueue.offer rather than BlockingQueue.put, or am I missing 
> something?

 From the SynchronousQueue javadocs:

"You cannot peek at a synchronous queue because an element is only present 
when you try to take it; you cannot add an element (using any method) 
unless another thread is trying to remove it; you cannot iterate as there 
is nothing to iterate."

SynchronousQueue is a strange beast, but it's what you want here.

--tim



More information about the Concurrency-interest mailing list