Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Thread's interrupt() method

 
vishal mishra
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Namaste Friends !!

Please explain what happens to a thread if its interrupt() method is called ??

In the following program


What is happening to the thread ??

If a thread is interrupted what happens then ?Does it stops its execution or carry on execution even after interruption??

What would be thread's state if it gets interrupted?? Does its state changes back to runnable or it gets terminated??

Version 6 Java docs for InterruptedExceptoin
http://docs.oracle.com/javase/6/docs/api/java/lang/InterruptedException.html says

InterruptedException Thrown when a thread is waiting, sleeping, or otherwise occupied, and the thread is interrupted, either before or during the activity


If method throws InterruptedException when thread gets interrupted why the run() method above is not throwing InterruptedException ??

lots of questions.....

I know it takes time for explaining me these questions........please help me...

Thanks



 
Paweł Baczyński
Bartender
Posts: 1822
33
Firefox Browser IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The exception is not thrown because the Thread is not sleeping, joining anything etc.
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's interesting that the output is the same with the interrupt(); call or without -


Regards,
Dan
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can try -



Regards,
Dan
 
Henry Wong
author
Marshal
Pie
Posts: 21498
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vishal mishra wrote:Namaste Friends !!

Please explain what happens to a thread if its interrupt() method is called ??



What is happening to the thread ??

If a thread is interrupted what happens then ?Does it stops its execution or carry on execution even after interruption??

What would be thread's state if it gets interrupted?? Does its state changes back to runnable or it gets terminated??

Version 6 Java docs for InterruptedExceptoin
http://docs.oracle.com/javase/6/docs/api/java/lang/InterruptedException.html says

InterruptedException Thrown when a thread is waiting, sleeping, or otherwise occupied, and the thread is interrupted, either before or during the activity


If method throws InterruptedException when thread gets interrupted why the run() method above is not throwing InterruptedException ??

lots of questions.....

I know it takes time for explaining me these questions........please help me...



Simply, as mentioned by other ranchers, if the thread is doing a wait(), sleep(), join(), etc., then an InterruptedException will be thrown.

Also, if the thread is doing IO, and the platform supports interrupted IO (such as windows), then an InterruptedIOException will be thrown.

Otherwise... it is cooperative. The thread must check the state of the interrupt flag, and deal with it. If the thread doesn't check the flag, and doesn't do any operation that can throw an InterruptedException, then nothing will happen differently -- which is what is happening with this example.

Henry

 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vishal,

A nice example can be found at Need sample program to throw InterruptedException -


Its output is -


I was interrupted!


Regards,
Dan
 
vishal mishra
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you friends for your good explanations and examples.

I have understood that on being interrupted thread leaves what it is currently doing such as sleeping , waiting or doing specific I/O operations,but still it is runnable and can execute its normal instructions, as it is doing in above examples.....


Please tell me I am right or wrong...if wrong then where...??

Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic