aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes interrupt() method! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "interrupt() method!" Watch "interrupt() method!" New topic
Author

interrupt() method!

ashok khetan
Ranch Hand

Joined: Jul 29, 2001
Posts: 153
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

Joined: Nov 03, 2000
Posts: 267
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

Joined: Nov 03, 2000
Posts: 267
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

Joined: Sep 21, 2001
Posts: 13
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: interrupt() method!