what to do about InterruptedException in ThreadPoolExecutor.awaitTermination

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

what to do about InterruptedException in ThreadPoolExecutor.awaitTermination

Alexandru Popescu ☀
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.

thanks in advance for all your advise,

./alex
--
.w( the_mindstorm )p.
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: what to do about InterruptedException inThreadPoolExecutor.awaitTermination

Dawid Kurzyniec
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

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: what to do about InterruptedException inThreadPoolExecutor.awaitTermination

David Holmes-3
In reply to this post by Alexandru Popescu ☀
Alex,

> 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

So the difference between #2 and #3 in practice is what else the thread
executing the above code does. In general you should always do #2 - it is
safe and correct no matter what thread does. #3 can be correct if the thread
does nothing else after the above code, but if you/someone changes that
later then they need to remember to convert to #2 if needed. So stick with
#2 as a general rule.

Cheers,
David Holmes

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: what to do about InterruptedException inThreadPoolExecutor.awaitTermination

Alexandru Popescu ☀
In reply to this post by Dawid Kurzyniec
On 10/18/06, Dawid Kurzyniec <[hidden email]> 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.

./alex
--
.w( the_mindstorm )p.

> Regards,
> Dawid
>
>
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: what to do about InterruptedException inThreadPoolExecutor.awaitTermination

Alexandru Popescu ☀
In reply to this post by David Holmes-3
On 10/18/06, David Holmes <[hidden email]> wrote:

> Alex,
>
> > 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
>
> So the difference between #2 and #3 in practice is what else the thread
> executing the above code does. In general you should always do #2 - it is
> safe and correct no matter what thread does. #3 can be correct if the thread
> does nothing else after the above code, but if you/someone changes that
> later then they need to remember to convert to #2 if needed. So stick with
> #2 as a general rule.
>

Considering my previous answer to  Dawid, it looks like at least I got
this one right.

./alex
--
.w( the_mindstorm )p.

> Cheers,
> David Holmes
>
>
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: what to do about InterruptedExceptioninThreadPoolExecutor.awaitTermination

Dawid Kurzyniec
In reply to this post by Alexandru Popescu ☀
Alexandru Popescu wrote:
>> 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.
>  

There is also future.get(timeout)...

Regards,
Dawid

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: what to do about InterruptedExceptioninThreadPoolExecutor.awaitTermination

Alexandru Popescu ☀
On 10/18/06, Dawid Kurzyniec <[hidden email]> wrote:

> Alexandru Popescu wrote:
> >> 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.
> >
>
> There is also future.get(timeout)...
>

It looks like by the time I initially wrote the code (and even now) I
wasn't aware of this one. Thanks.

./alex
--
.w( the_mindstorm )p.

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