[concurrency-interest] question about ExecutorCompletionService

Tim Peierls tim at peierls.net
Thu Aug 31 14:31:00 EDT 2006

On 8/31/06, Doug Lea <dl at cs.oswego.edu> wrote:
> We could in fact compatibly add method getExecutor() because
> this is a concrete class and so doesn't impact other interfaces
> etc. And it would be handy in those cases where you
> don't otherwise have a reference to the executor used to create
> the ExecutorCompletionService. But unfortunately, nearly all
> uses of this would require casting of result, making it
> a bit ugly. Perhaps there is some better way of going about
> this with equivalent but nicer effect?

It's not hard to roll your own:

public class TrackedExecutorCompletionService<V, E extends Executor>
        extends ExecutorCompletionService<V> {

    private final E exec;

    public TrackedExecutorCompletionService(E exec) {
        this.exec = exec;

    public TrackedExecutorCompletionService(
            E exec, BlockingQueue<Future<V>> queue) {
        super(exec, queue);
        this.exec = exec;

    public E getExecutor() { return exec; }


On 8/31/06, Doug Lea <dl at cs.oswego.edu> wrote:
> yangjs wrote:
> > hi,all
> >   I have a Consumer poll task from ExecutorCompletionService ,this
> > class has  no method such as /isTerminated/, when app is shutdown, I
> > don't know when to shutdown my Consumer ,even if poll return null,.I
> > can't accept consumer lost task . if  ExecutorCompletionService have
> > method isTerminated as follow,thing will be simple. I can check when
> > service poll return null and service isTerminated, my consumer can
> > shutdown safely. is right??
> > why not provide such method ?
> >
> > public boolean isTerminated(){
> >    return executor.isTerminated();
> >  }
> >
> >
> This is a pretty good suggestion; thanks!
> Except that ExecutorCompletionService requires only an Executor,
> not an ExecutorService, and Executors don't necessarily
> have lifetime control methods (shutdown, isTerminated, etc).
> So in cases where people use some custom Executor that is
> not also an ExecutorService, the method can't be implemented.
> So the best we could do is add method
>    public Executor getExecutor() { return executor; }
> And you would then need to check termination using the ugly:
>    ((ExecutorService)(ecs.getExecutor()).isTerminated();
> (and being prepared for cast failure.)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/attachments/20060831/41c20b5f/attachment.html 

More information about the Concurrency-interest mailing list