This week's book giveaway is in the HTML/CSS/JavaScript forum.
We're giving away four copies of Practical SVG and have Chris Coyier on-line!
See this thread for details.
Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Method interrupt()

 
Zahid, Butt
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reanimating a post from 2001? I don't think it's URGENT enough to do that.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!