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

Jonathan Baxter jbaxter at panscient.com
Tue May 17 19:46:38 EDT 2005

On Wednesday 18 May 2005 08:34, Tim Peierls wrote:
> 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.

But ThreadPoolExecutor uses the the offer method not the put method of the 
BlockingQueue, and according to the BlockingQueue documentation, offer 
returns immediately if an element cannot be added to the queue. So it doesn't 
seem to matter what kind of BlockingQueue you specify (Synchronous or 
otherwise), you're never going to get blocking behaviour. 

Looking at the source of the old PooledExecutor, the waitWhenBlocked method 
installed a BlockedExecutionHandler that explicitly invokes the put method on 
the queue. So to get the same behaviour from ThreadPoolExecutor I guess one 
could install a RejectedExecutionHandler that does the same thing, but 
explicitly accessing the BlockingQueue in the RejectedExecutionHandler seems 

- Jonathan 

> --tim

More information about the Concurrency-interest mailing list