t.interrupt() 问题 本帖最后由 jn0115 于 2014-05-30 22:59:38 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 刚对线程有些研究,Thread2没有自己中断的可能无法释放锁所以应该是死锁了,只能强制终止。调用thread1的interrupt方法可以改变线程的中断状态,但是由于没有获得锁无法抛出InterruptedException异常。就像notify一样,只有退出同步块放弃了锁才能唤醒一个等待的线程。个人观点,可能有误。测试的代码:public class Interrupt { Object lock = new Object(); public static void main(String[] args) throws InterruptedException { new Interrupt().foo(); } Thread thread1; Thread thread2; private void foo() throws InterruptedException { thread1 = new Thread1(); thread2 = new Thread2(); thread1.start(); thread2.start(); new Thread() { { setDaemon(true); } @Override public void run() { boolean b1 = false; boolean b2 = false; while (true) { if (b1 != (b1 = thread1.isInterrupted())) System.out.println("thread1.isInterrupted()" + thread1.isInterrupted()); if (b2 != (b2 = thread2.isInterrupted())) System.out.println("thread2.isInterrupted()" + thread2.isInterrupted()); try { Thread.sleep(10); } catch (InterruptedException e) {} } } }.start(); try { Thread.sleep(5000); } catch (InterruptedException e) {} thread2.interrupt(); // thread2.stop(); } class Thread1 extends Thread { @Override public void run() { boolean once = true; synchronized (lock) { for (int i = 0; i < 10; i++) { System.out.println("Thread1 running."); if (once) { once = false; try { lock.wait(); } catch (InterruptedException e) { System.out.println("wait interrupted,isInterrupted=false"); } } try { Thread.sleep(500); } catch (InterruptedException e) {} } } } } class Thread2 extends Thread { @Override public void run() { boolean once = true; synchronized (lock) { while (true) { System.out.println("Thread2 running"); try { Thread.sleep(500); if (once) { once = false; System.out.println("Interrupt Thread1."); thread1.interrupt(); // lock.notify(); } } catch (InterruptedException e) { System.out.println("Thread2 end."); return; } } } } }} Thread1 running.Thread2 runningInterrupt Thread1.Thread2 runningthread1.isInterrupted()trueThread2 runningThread2 runningThread2 runningThread2 runningThread2 runningThread2 runningThread2 runningThread2 runningThread2 end.wait interrupted,isInterrupted=falsethread1.isInterrupted()falseThread1 running.Thread1 running.Thread1 running.Thread1 running.Thread1 running.Thread1 running.Thread1 running.Thread1 running.Thread1 running. 关于计算两个日期之间的天数 一个用Java写的关于完全二叉树的问题 简单的for问题,帮帮忙。。。 eclipse工程中为什么看不到工程里面的bin目录及bin目录里面的class文件? gui问题,急等! 高分_关于发布自己的程序 哪里有apache+weblogic的安装的介绍,谢谢 随机数问题 请问在jb调试时可以把weblogic放在另一台机器上吗? 请问能修改hashtable里的内容吗? 关于jdbc中date类型的处理 java中,想用Vector实现查询数据库,然后表格输出,怎么查询界面不能输出表格,连表格头都输不出来??
测试的代码:public class Interrupt {
Object lock = new Object(); public static void main(String[] args) throws InterruptedException {
new Interrupt().foo();
} Thread thread1;
Thread thread2; private void foo() throws InterruptedException {
thread1 = new Thread1();
thread2 = new Thread2();
thread1.start();
thread2.start();
new Thread() {
{
setDaemon(true);
} @Override
public void run() {
boolean b1 = false;
boolean b2 = false;
while (true) {
if (b1 != (b1 = thread1.isInterrupted()))
System.out.println("thread1.isInterrupted()" + thread1.isInterrupted()); if (b2 != (b2 = thread2.isInterrupted()))
System.out.println("thread2.isInterrupted()" + thread2.isInterrupted());
try {
Thread.sleep(10);
} catch (InterruptedException e) {}
}
}
}.start(); try {
Thread.sleep(5000);
} catch (InterruptedException e) {}
thread2.interrupt();
// thread2.stop();
}
class Thread1 extends Thread {
@Override
public void run() {
boolean once = true;
synchronized (lock) {
for (int i = 0; i < 10; i++) {
System.out.println("Thread1 running.");
if (once) {
once = false;
try {
lock.wait();
} catch (InterruptedException e) {
System.out.println("wait interrupted,isInterrupted=false");
}
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {}
}
}
}
}
class Thread2 extends Thread {
@Override
public void run() {
boolean once = true;
synchronized (lock) {
while (true) {
System.out.println("Thread2 running");
try {
Thread.sleep(500);
if (once) {
once = false;
System.out.println("Interrupt Thread1.");
thread1.interrupt();
// lock.notify();
}
} catch (InterruptedException e) {
System.out.println("Thread2 end.");
return;
}
}
}
}
}
}
Thread2 running
Interrupt Thread1.
Thread2 running
thread1.isInterrupted()true
Thread2 running
Thread2 running
Thread2 running
Thread2 running
Thread2 running
Thread2 running
Thread2 running
Thread2 running
Thread2 end.
wait interrupted,isInterrupted=false
thread1.isInterrupted()false
Thread1 running.
Thread1 running.
Thread1 running.
Thread1 running.
Thread1 running.
Thread1 running.
Thread1 running.
Thread1 running.
Thread1 running.