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

Alexandru Popescu the.mindstorm.mailinglist at gmail.com
Tue Oct 17 20:36:56 EDT 2006

On 10/18/06, Dawid Kurzyniec <dawidk at mathcs.emory.edu> wrote:
> 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.

Thanks. I guess this is true only in case the thread is reused
somehow, and not if the thread is never used again.

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

The reason I need an awaitTimeout is cause I must be able to timeout the method.

.w( the_mindstorm )p.

> Regards,
> Dawid

More information about the Concurrency-interest mailing list