[concurrency-interest] what to do about InterruptedException inThreadPoolExecutor.awaitTermination

Dawid Kurzyniec dawidk at mathcs.emory.edu
Tue Oct 17 20:23:39 EDT 2006


Alexandru Popescu wrote:
> Hi!
>
> I am using a ThreadPoolExecutor to run invoke concurrently different
> methods on an object. Before continuing, I am interested if the task
> was finished:
>
> My code looks something like:
>
> [code]
> exec.submitRunnable(imr);
> exec.shutdown();
> boolean finished= exec.awaitTermination(timeOut);
> [/code]
>
> I am trying to understand what should I do about the
> InterruptedException that can be thrown from the awaitTermination
> method. As I read in the JCiP, I should be doing one of:
> 1/ propagate it (which is not acceptable in my case)
> 2/ have something like Thread.currentThread().interrupt();
> 3/ ignore it
>
> Currently, because I couldn't find a good answer to my question, I am
> just ignoring it and it looks like things are working as expected (and
> I have a couple of test covering the code). I have also tried the 2nd
> option and I am getting the same results, so I am wondering if I am
> safe with the 3rd option or should I do the 2nd one.
>   

If you can't do 1) you should always do 2), so that later in the same 
thread (e.g. when you return from the method) the interruption status is 
not lost and eventually results in InterruptedException once the thread 
enters another blocking call. But I am not clear why you need to 
shutdown the executor in the code above. Do you want to wait for _all_ 
submitted tasks to finish, or just the one you just submitted? In the 
latter case, you should call get() on the future object returned from 
submit(), instead of awaitTermination on the executor.

Regards,
Dawid



More information about the Concurrency-interest mailing list