[concurrency-interest] blocking executor

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


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.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/attachments/20060911/fe5a2cfd/attachment-0001.html 


More information about the Concurrency-interest mailing list