• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

interrupt() method!

 
ashok khetan
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi! why the statement at //1 shows false, even the thread is interrupted by thread of class thr?? afaik, when a thread is interrupted, JVM sets the interrupt flag of thread true, so at //1, it should display true.
thanx.
ashok.
_________
class thr implements Runnable {
Thread t;
thr(Thread t) {
this.t = t;
}
public void run() {
System.out.println("in thr..run");
t.interrupt();
System.out.println("after thr.. run");
}
}
public class fort extends Thread {
public void run() {
System.out.println("in fort..run");
try {
sleep(100);
} catch(InterruptedException ie) {
System.out.println("interrupted");
}
System.out.println(this.isInterrupted()); //1
System.out.println("retd. from interruption..bye");
}
public static void main(String[] args) {
fort f = new fort();
thr t1 = new thr(f);
f.start();
new Thread(t1).start();
System.out.println("in Main");
}
}
 
Roopa Bagur
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are interupting t1 & not f.. I think thats why..
Originally posted by ashok khetan:
hi! why the statement at //1 shows false, even the thread is interrupted by thread of class thr?? afaik, when a thread is interrupted, JVM sets the interrupt flag of thread true, so at //1, it should display true.
thanx.
ashok.
_________
class thr implements Runnable {
Thread t;
thr(Thread t) {
this.t = t;
}
public void run() {
System.out.println("in thr..run");
t.interrupt();
System.out.println("after thr.. run");
}
}
public class fort extends Thread {
public void run() {
System.out.println("in fort..run");
try {
sleep(100);
} catch(InterruptedException ie) {
System.out.println("interrupted");
}
System.out.println(this.isInterrupted()); //1
System.out.println("retd. from interruption..bye");
}
public static void main(String[] args) {
fort f = new fort();
thr t1 = new thr(f);
f.start();
new Thread(t1).start();
System.out.println("in Main");
}
}

 
Roopa Bagur
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I reduced the sleep time to 0 & it returned true..still investigating the behaviour will let you know.
Originally posted by ashok khetan:
hi! why the statement at //1 shows false, even the thread is interrupted by thread of class thr?? afaik, when a thread is interrupted, JVM sets the interrupt flag of thread true, so at //1, it should display true.
thanx.
ashok.
_________
class thr implements Runnable {
Thread t;
thr(Thread t) {
this.t = t;
}
public void run() {
System.out.println("in thr..run");
t.interrupt();
System.out.println("after thr.. run");
}
}
public class fort extends Thread {
public void run() {
System.out.println("in fort..run");
try {
sleep(100);
} catch(InterruptedException ie) {
System.out.println("interrupted");
}
System.out.println(this.isInterrupted()); //1
System.out.println("retd. from interruption..bye");
}
public static void main(String[] args) {
fort f = new fort();
thr t1 = new thr(f);
f.start();
new Thread(t1).start();
System.out.println("in Main");
}
}

 
galen wang
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it seems like when thread responds an interrupt, it will clear this thread's interrupt status first, so after the running flow enter the catch(InterruptedException ie) block, you can not be able to test the isInterrupted() as true any more.
below is modified code, it may help to understand.

the result is
in Main
in fort..run
t now sleep
in thr..run
outside t: befor call t.interrupt(), the interrupt status is false
-- inside t: befor call super.interrupt(), the interrupt status is false
-- inside t: after call super.interrupt(), the interrupt status is true
outside t: after call t.interrupt(), the interrupt status is true
outside t: waiting for t response ...
-- inside t: respond for catching InterruptedException, now the interrupt status is false
-- inside t: but interrupted is catched
-- inside t: false
-- inside t: retd. from interruption..bye
outside t: after t t.responded(), the interrupt status is false
after thr.. run
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic