• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Quest : Interrupt, isInterrupted, interrupted

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

Please go throught the below code


My question is on line #1 we have interruptd the thread t, which is sleeping for 10secs. Then why does isInterrupted() on t returns false.

from the java API at http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.html#interrupt()

I understood that , if a thread is interrupted it should return true otherwise it should return false. But in our case, we have actually interrupted the thread then why did isInterrupted() return false. Please clarify me on this.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it's because when you try to use sleep() on a slept Thread,fine,A InterruptedException is thrown as the slept Thread's interrupted state being remove ,so it returns false when you use t.isInterrupted();
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So catching the exception caused isInterrupted() to return false?

I have modified the program so that the thread is interrupted while running without using sleep() and catch(). Now the isInterrupted() returns true.
please clarify.



output:
started
Interrupting
true
ended
[ September 23, 2007: Message edited by: Ahmed Yehia ]
 
Fusheng Xiao
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is , When you call sleep(); twice on a Thread,the isInterrupted is being removed . but you call interrupt() after sleep().JVM does not automaticely remove the isInterrupted() .so the isInterrupted() return true.
Shortly speaking,isInterrupted() change into true while calling interrupt().but not changed into false while calling interrupt() on a interrupted Thread.
Hoping you can understand . you can check on the API to get more information.
 
All of life is a contant education - Eleanor Roosevelt. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic