• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interrupt()

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic