wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes interrupt() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "interrupt()" Watch "interrupt()" New topic
Author

interrupt()

Tony kunds
Greenhorn

Joined: Apr 25, 2002
Posts: 25
how can the statement interrupt() forces the thread to stop() is true
i appreciate any link to interrupt() and InterruptedException.
I know that interrupt() is called on a sleeping thread.
my question is when will a thread throw an InterruptedException and when will it stop the Thread.
-thanks in advance
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
In general, interrupting a thread is the proper way to "stop it." In fact, an interruption, by its very nature, is a great way of "getting the attention" of a thread, whether it is sleeping or processing. Take the following example:

You'll notice that, in either case, you can break out of the run method by interrupting the thread. The difference is that, if the thread is not sleeping, you'll have to do a check on your own, like I did here. Also, if you get stuck in a long loop, it's possible that you might not check for the interruption for some time, as was the case in this example. Therefore, if you have a thread that you'd like to be able to interrupt often, you'll want to design that thread with that in mind.
I hope this helps,
Corey


SCJP Tipline, etc.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
A thread on which Object.wait , Thread.join or Thread.sleep were invoked can be interrupted. All of these methods must be used knowing that they could throw a cheked exception: InterruptedException if Thread.interrup is invoked. Otherwise it's possible to check as, Corey did, with Thread.isInterrupted


SCJP2. Please Indent your code using UBB Code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: interrupt()
 
Similar Threads
Thread
Stopping a thread via command line
Valid Statement
stop a thread (help Rahul M. )
help needed plz