本帖最后由 jn0115 于 2014-05-30 22:59:38 编辑

解决方案 »

  1.   

    刚对线程有些研究,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;
                        }
                    }
                }
            }
        }
    }
      

  2.   

    Thread1 running.
    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.