blocking executor

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

blocking executor

Jed Wesley-Smith
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
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: blocking executor

Jed Wesley-Smith
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
> [hidden email]
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
>  

--
cheers,
- jed.

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest