• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

try-with-resources inside a thread and InterruptedException

 
Greenhorn
Posts: 28
Netbeans IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?


 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need 'sleep' or 'wait' for this to work.
 
Mark Jame
Greenhorn
Posts: 28
Netbeans IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you share the code?
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess I might just as well not have replied.
 
Mark Jame
Greenhorn
Posts: 28
Netbeans IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Piet.
 
No one can make you feel inferior without your consent - Eleanor Roosevelt. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic