• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

interrupt() method can kill a thread??

 
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
basha khan
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 516
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
afterall i understood
thanks to everybody
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic