• Post Reply Bookmark Topic Watch Topic
  • New Topic

try-with-resources inside a thread and InterruptedException

 
Mark Jame
Greenhorn
Posts: 28
Java Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think I understand something about try-with-resources so I have two questions.

Suppose I have a class called MyWriter that extends Thread and I have a try-with-resources in the run method; lets say I create file and buffered output streams to serialize a list of objects.

If I create a MyWriter (mw for example), what happens to the InterruptedException inside MyWriter if I call mw.interrupt()?

Do I ever have to think about InterruptedException if I use try-with-resources inside a run method?


 
K. Tsang
Bartender
Posts: 3610
16
Firefox Browser Java Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Jame wrote:
If I create a MyWriter (mw for example), what happens to the InterruptedException inside MyWriter if I call mw.interrupt()?

Do I ever have to think about InterruptedException if I use try-with-resources inside a run method?



I don't think it matters if the thread is inside a try-with-resources block or not. For your first question, I believe you are asking can the thread interrupt itself. Have a look at this thread that discusses this possibility.
 
Mark Jame
Greenhorn
Posts: 28
Java Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick reply K. Tsang, but that is not my question.

NOTE: the following is designed only to help my understanding of some things, I would not neccessarily design like this for real.

I know I can interrupt my thread class if I wish, for example with mw.interrupt().

If I implement my run method using a normal try-catch clause, I can catch things like IO execptions and also InterruptedException and handle both individually taking whatever action I choose.

But, if I implement my run method using try-with-resources I cannot catch InterruptedException, where does it go and/or where can I catch it?

Thanks.
 
Piet Souris
Rancher
Posts: 1634
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can simply call the methof 'isInterrupted()' to see if the thread is interrupted,
and act however you see fit.

Greetz,
Piet
 
Roel De Nijs
Sheriff
Posts: 10763
147
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Jame wrote:But, if I implement my run method using try-with-resources I cannot catch InterruptedException, where does it go and/or where can I catch it?

Says who? A try-with-resources statement can have catch and finally blocks just like an ordinary try statement. In a try-with-resources statement, any catch or finally block is run after the resources declared have been closed.

This is perfectly legal code. Catching an InterruptedException using a try-with-resources:
 
Piet Souris
Rancher
Posts: 1634
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need 'sleep' or 'wait' for this to work.
 
Mark Jame
Greenhorn
Posts: 28
Java Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roel

Your example is exactly what I thought I could do, however when I try to do that I get an error (via Eclipse):

Unreachable catch block for InterruptedException. This exception is never thrown from the try statement body.

Very strange, any ideas?
 
Roel De Nijs
Sheriff
Posts: 10763
147
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you share the code?
 
Piet Souris
Rancher
Posts: 1634
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess I might just as well not have replied.
 
Mark Jame
Greenhorn
Posts: 28
Java Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:I guess I might just as well not have replied.

Hold on, still taking in all the feedback; it is all very much appreciated, I promise.
 
Roel De Nijs
Sheriff
Posts: 10763
147
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Jame wrote:Your example is exactly what I thought I could do, however when I try to do that I get an error (via Eclipse):

Unreachable catch block for InterruptedException. This exception is never thrown from the try statement body.

This will only compile if in the try-block you invoke a method which declares to throw an InterruptedException. If you don't, you'll get exactly the compiler error you mentioned in this post.

Note: This has nothing to do with try-with-resources, with a regular try-block you'll see exactly the same behavior.
 
Mark Jame
Greenhorn
Posts: 28
Java Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I must apologise because checking my examples, I had a sleep in my original run method that I commented out.

Assuming I cannot have a catch clause for InterruptedException (because nothing in the run method throws it); how can I handle interrupt being called on my thread? For example if my thread class is in an API and some code calls myThread.interrupt(); how can my run method get notified so it can exit quickly?

Piet, I assume the only way is to check Thread.interrupted() in my run method?
 
Piet Souris
Rancher
Posts: 1634
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Mark,

well, I wouldn't say it's the only way, but it is the easiest way.
Something like:

Greetz,
Piet
 
Mark Jame
Greenhorn
Posts: 28
Java Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Piet.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!