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.
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
posted
0
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
posted
0
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
posted
0
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.