Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Thread interrupt

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Quoted from RHE:

A sleeping thread that recieves an interrupt() call moves immediately into the Ready state; when it gets to run, it will execute its InterruptedException handler.


<pre><font color=#7b007b>
What does it mean "it will execute its InterruptedException handler?" Does it mean an InterruptedException is being caught? Why doesn't it continue to run the run() method?
Thanks,
Jenny
</pre></font>
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you call sleep, you must catch the InterruptedException or declare that your method throws it:
 
Jian Yi
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rob,
Thanks for both of your replies. Your code clarifies the question on InterruptedException. Can you answer my other question of whether the sleeping Thread continues to run after its sleep being interrupted?
Thanks,
Jenny
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is up to you and how you write your run() method.
If you write this:

Then after this thread sleeps, the run() method ends. Also, if the thread is interrupted while sleeping, it will end.
This, however, will continue after being interrupted:


And this thread will stop after being interrupted:

So you see, it's completely up to you!
[ March 12, 2002: Message edited by: Rob Ross ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic