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 interrupt() method can kill a thread?? 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 "interrupt() method can kill a thread??" Watch "interrupt() method can kill a thread??" New topic
Author

interrupt() method can kill a thread??

basha khan
Ranch Hand

Joined: Jan 26, 2002
Posts: 516
look this mock question
Which statements regarding the following code are correct?
class A extends Thread{
public void run(){
System.out.println("starting loop");
while(true){};//(1)
System.out.println("ending loop");
}
}
public class TestClass {
public static void main(String asd[]){
A a=new A();
a.start();
Thread.sleep(1000);
a.interrupt();//(2)
}
}
---------------------------------------
answer is
it will run and end cleanly if //(1) is replaced to while(!isInterrupted())

why it is possible?
The user thread is interrupted at (2) it is not stopped
so how the program will quit?
can interrupt method stop a Thread??.
if interrupt can,plz tell me about interrupt() behavior
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5369
when u interrupt a thread ... interupted thread can take any action .. if it wants to take by checking isInterupted() method..
here in your prog.. if while is changed with
while(!isInterrupted()) then when it will be interrupted then it will return true .. which in turn becomes false(note ! operator).. so loop won't execute any more ...
so thread will end.
CMIW
HIH


"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
chung lee
Ranch Hand

Joined: Jan 17, 2002
Posts: 33
Hi Basha,
Just to add to what Ravish has said, the main method is a thread which spawns the class A thread.
So when the thread A is executing in the
while(!isInterrupted()){}
it is waiting for an interrupt signal from the main thread which sleeps for at least 1000 milliseconds before calling interrupt() on thread A.
chung
Bob Graffagnino
Ranch Hand

Joined: May 30, 2001
Posts: 81
I was under the impression that a call to a Thread object's interrupt() method sets a isInterrupted flag to true. If the program never checks the flag, it won't stop.
The funny thing is, neither the Thread class nor the Object class have an isInterrupted field. Anyone care to explain this to me?
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Better check your javadocs again. This method is clearly documented!!

public boolean isInterrupted()
Tests whether this thread has been interrupted. The interrupted status of the thread is unaffected by this method.
Returns:
true if this thread has been interrupted; false otherwise.

Rob


Rob
SCJP 1.4
basha khan
Ranch Hand

Joined: Jan 26, 2002
Posts: 516
a few questions from myself not pointed the correct target.this is the latest
my questien was not on isInterrupted() method which comes on //(1)
my questien was on interrupt() on // (2)
here is my questien,
----------------------------------------
class A extends Thread{
public void run(){
System.out.println("starting loop");
while(!isInterruted()){}//(1)
System.out.println("ending loop");
}
}
public class TestClass {
public static void main(String asd[]){
A a=new A();
a.start();
Thread.sleep(1000);//(a)
a.interrupt();//(2)
}
}
---------------------------------------
user thread a will start when main thread sleeps(//a)
thread 'a' will enter into run() and check
!isInterrupted(),the condition is true now.
after the sleeping period of main,thread sheduler have two options
opt(1)main thread can enter on run state,which will make thread 'a' interrupted.so thread 'a' not stopped.
opt(2)user thread can continue,it executes an endless loop.so there is no way to terminate.
above two options, the program will not terminate unless the interrupt() method can kill the thread 'a'
my questien is whether interrupt() method can kill a thread??.if so,plz tell me the behavior of interrupt method?.anyone plz..
chung lee
Ranch Hand

Joined: Jan 17, 2002
Posts: 33
Hi Basha,
I think that you're confusing yourself here. Please feel free to correct me if I'm wrong, but the activity of interrupt() does not kill the thread. Threads may only die when the run() method returns (or if someone turns off your computer etc).
As Rob has correctly pointed out, interrupt() will cause an internal state change within the thread and the boolean "interrupt" flag will change from false to true when the thread is interrupted. This has no affect on the current activity of the running thread.
If interrupt() caused the running thread to stop and/or die, you will be faced with alot of problems. See the deprecated methods of stop(), resume() and suspend() for the full explanation.
I hope that helps.
chung
basha khan
Ranch Hand

Joined: Jan 26, 2002
Posts: 516
afterall i understood
thanks to everybody
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: interrupt() method can kill a thread??
 
Similar Threads
co-ordination between join(), start(), sleep(), interrupt()
Thread Ques
Threads
Interrupt Confusion
threads