aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in Thread's interrupt() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in Thread Watch "Doubt in Thread New topic
Author

Doubt in Thread's interrupt() method

vishal mishra
Ranch Hand

Joined: Jul 12, 2010
Posts: 103
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

Joined: Apr 18, 2013
Posts: 975
    
  16

The exception is not thrown because the Thread is not sleeping, joining anything etc.


Formely Pawel Pawlowicz
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1180
It's interesting that the output is the same with the interrupt(); call or without -


Regards,
Dan

William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1180
You can try -



Regards,
Dan
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18845
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1180
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

Joined: Jul 12, 2010
Posts: 103
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in Thread's interrupt() method