This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Method interrupt() Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Method interrupt()" Watch "Method interrupt()" New topic
Author

Method interrupt()

Zahid, Butt
Ranch Hand

Joined: Feb 16, 2001
Posts: 100
Hi ALL

I am relatively new to Threads. What does the method interrupt()
do to a Thread ??
Is this a static method ??
How and Why would you use this method ??

Prompt reply will be highly appreciated as this is Urgent.

Thanks in advance.
[ July 26, 2006: Message edited by: Jim Yingst ]
Rahul Rathore
Ranch Hand

Joined: Sep 30, 2000
Posts: 324
interrupt() is not a static method. One thread sends an interrupt to another thread t by calling t.interrupt().
t.interrupt() has the following effect:-
1. if t is running then the only effect is set an interrupted flag in t. If after the interrupted flag is set, there is an attempt to make t go into waiting by calling sleep() or wait(), then it immediately throws InterruptedException.
2. if t is waiting when interrupt is received, then it throws InterruptedException, and awakens.
interrupt is normally sent for stopping/suspending threads. Note that interrupt by itself does NOT stop/suspend thread. Rather it used for this purpose. To understand how it may be used to stop threads see Peter Hagar's post in http://www.javaranch.com/ubb/Forum27/HTML/000247.html
Since stop() and suspend() are deprecated, the above idiom is the recommended way of stopping/suspending threads.
There are a lot of old posts explaining the role of interrupt. I myself learnt a lot from Maha Anna's post.
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Where can this behavior be found? Its not in the description of interrupt() and I can't so far find it in the JLS or JVM spec.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Reanimating a post from 2001? I don't think it's URGENT enough to do that.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
I removed the pointless "URGENT" from the title. CLG, most of the behavior above is described in the Thread API, isn't it? Not just under interrupt() - there are various references to "interrupt status" and "interrupted status" throughout the class. Some details are covered under wait() or sleep() instead. What particular part or parts above are you questioning?


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method interrupt()
 
Similar Threads
threads
Dan's Question : Thread
Interrupt an executing method
MultiThreaded Programming: Killing Thread
Thread stop