日期:2014-05-20 浏览次数:20728 次
public class testThread {
Thread t = null;
public void setT(Thread t) {
this.t = t;
}
class threadA implements Runnable{
private void paint() {
for(int i = 0; i < 20; i ++) {
System.out.println("a线程-- " + t.isInterrupted());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void run() {
synchronized (this) {
try {
this.wait(5000);
} catch (InterruptedException e) {
paint();
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
testThread test = new testThread();
Thread t = new Thread(test.new threadA());
test.setT(t);
t.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("中断之前- " + t.isInterrupted());
t.interrupt();
for(int i = 0; i < 20; i ++) {
System.out.println("主线程--" + t.isInterrupted());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}