This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Quest : Interrupt, isInterrupted, interrupted Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Quest : Interrupt, isInterrupted, interrupted" Watch "Quest : Interrupt, isInterrupted, interrupted" New topic
Author

Quest : Interrupt, isInterrupted, interrupted

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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.
Fusheng Xiao
Greenhorn

Joined: Sep 22, 2007
Posts: 7
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();
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424
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

Joined: Sep 22, 2007
Posts: 7
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Quest : Interrupt, isInterrupted, interrupted
 
Similar Threads
Synchronization practice: why does thread1 rush through loop without pause or reset?
Threading
Can interrupt() method is used to interrupt running thereads ...
About thread and annonumous inner class
isInterrupted() or interrupted() !!!!