请问一下,线程里面的interrupt()方法里面具体是怎么实现的,为什么它能对线程打断?多线程

解决方案 »

  1.   

    =================================
    像这种问题,直接看源代码就可以了。
        public void interrupt() {
    if (this != Thread.currentThread())
        checkAccess(); synchronized (blockerLock) {
        Interruptible b = blocker;
        if (b != null) {
    interrupt0();
    b.interrupt();
    return;
        }
    }
    interrupt0();
        } private native void interrupt0();
      

  2.   

     public void interrupt() {
            if (this != Thread.currentThread())
                checkAccess();        synchronized (blockerLock) {
                Interruptible b = blocker;
                if (b != null) {
                    interrupt0();           // Just to set the interrupt flag
                    b.interrupt(this);
                    return;
                }
            }
            interrupt0();
        }
    这是源码,它不是打断线程,而是清除中断线程,让线程回到运行状态,你可以查手册和源码,这样对学习是有好处的
      

  3.   

    =================================
    这个美女,这个方法是中断线程哟。至于怎么中断的,应该是 private native void interrupt0();
      

  4.   

    谢谢,但是亲,多线程的运行一般都是循环结构,而且结束线程就是结束run方法,interrupt()方法是结束线程的冻结状态,使线程回到运行中来,“如果线程在调用 Object 类的 wait()、wait(long) 或 wait(long, int) 方法,或者该类的 join()、join(long)、join(long, int)、sleep(long) 或 sleep(long, int) 方法过程中受阻,则其中断状态将被清除,它还将收到一个 InterruptedException。”这是API的解释,然后通过改变循环条件结束线程。是这样吧亲,