[concurrency-interest] blocking executor

Jed Wesley-Smith jed at atlassian.com
Mon Sep 11 04:59:23 EDT 2006


Sorry all, hit the send button too soon. The BoundedExecutor (JCiP 
Listing 8.4) is a better choice. I'll shut up now...

Jed Wesley-Smith wrote:
> All,
>
> I have been charged with implementing an ExecutorService that has a 
> bounded capacity, and if that capacity is reached will block the 
> caller. A CallerRunsPolicy is not desired here as the number of 
> concurrent tasks executing must be strictly held to the number of 
> threads in the pool, and the number of clients adding tasks is 
> indeterminate.
>
> Having looked at the code for a while, it seems a 
> BlockingRejectedExectionHandler should do the trick eg.
>
> RejectedExecutionHandler blocker = new RejectedExecutionHandler()
> {
>     public void rejectedExecution(Runnable r, ThreadPoolExecutor 
> executor)throws RejectedExecutionException
>     {
>         if (executor.isShutdown())
>         {
>             throw new RejectedExecutionException(executor + " is 
> shutdown!");
>         }
>         try
>         {
>             executor.getQueue().put(r);
>         }
>         catch (InterruptedException e)
>         {
>             Thread.currentThread().interrupt();
>             throw new RejectedExecutionException(e);
>         }
>     }
> });
>
> The question is, are there any better ways to do this, or are there 
> any obvious errors in the above.
>
> All advice greatly appreciated.
> -- 
> cheers,
> - jed.
>   
> ------------------------------------------------------------------------
>
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at altair.cs.oswego.edu
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
>   

-- 
cheers,
- jed.



More information about the Concurrency-interest mailing list