This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Interrupt()  RSS feed

 
Ian Montgomery
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I'm having a little trouble understanding exactly what interrupt() does. Does it simply set the interrupt flag and nothing else, leaving it up to the thread to deal with the flag?
The Passport book says, "An InterruptedException will be thrown when a thread is waiting or sleeping . . ." but then states later, "If the target thread was sleeping, waiting, . . . it will not even know that it was interrupted . . . the JVM will just set the interrupted flag . . . to be true."
Are the InterruptedException and the "interrupted flag" the same thing?
Thanks,
--Ian
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't have my copy of the Passport book with me right now to investigate, but the text in the form you show it is wrong. Either there's an error in the book, or you've misquoted. (What's in the "..." sections though? I can imagine several things that could go there which would change the meaning, so perhaps you should show a more complete quote, with page number.)
Anyway... no, InterruptedException is not the same thing as the interrupted flag. The JVM will use one of these two mechanisms to respond to an interrupt, but not both. If the interrupted thread is currently waiting or sleeping, then InterruptedException will be thrown, otherwise the interrupted flag will be set and no exception thrown. In the latter case it's up to the programmer to check the interrupted flag when it's convenient and respond appropriately.
Note that if the thread was not waiting or sleeping at the time the interrupt occurred, but enters this state later (without previously clearing the interrupt flag), then InterruptException will be thrown the moment the sleep/wait is entered, and the interrupt flag cleared. So in this case it's like the InterruptException is simply delayed until the wait/sleep actually occurs.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!