[concurrency-interest] question about ExecutorCompletionService

Doug Lea dl at cs.oswego.edu
Thu Aug 31 07:46:45 EDT 2006


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.)

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?

-Doug





More information about the Concurrency-interest mailing list