public class InterruptCheck extends Object {
public static void main(String[] args) {
Thread t = Thread.currentThread();
System.out.println("Point A: t.isInterrupted()=" + t.isInterrupted());
t.interrupt();
System.out.println("Point B: t.isInterrupted()=" + t.isInterrupted());
System.out.println("Point C: t.isInterrupted()=" + t.isInterrupted()); try {
Thread.sleep(2000);
System.out.println("was NOT interrupted");
} catch ( InterruptedException x ) {
System.out.println("was interrupted");
} System.out.println("Point D: t.isInterrupted()=" + t.isInterrupted());
}
}
(1)主线程已经中断了问什么程序还能往下执行???难道只有main线程有此特性??public class SleepInterrupt extends Object implements Runnable {
public static void main(String[] args) {
SleepInterrupt si = new SleepInterrupt();
Thread t = new Thread(si);
t.start(); // Be sure that the new thread gets a chance to 
// run for a while.
try { Thread.sleep(2000); } 
catch ( InterruptedException x ) { } System.out.println("in main() thread");
// t.interrupt();
System.out.println("t.isInterrupted()"+ t.isInterrupted());
System.out.println("in main() - leaving");
}
public void run() {
                  System.out.println("in run() - doing things after nap");
for(int i=0;i<100000;i++);
System.out.println("in run() - leaving normally");
         }

(2)为什么在线程t没有被interrupt的情况下,t线程没输出?

解决方案 »

  1.   

    我把第二个例子原来的.class文件删除重新编译就可以了,为什么??
    如果没有注释掉t.interrupt()那么System.out.println("t.isInterrupted()"+ t.isInterrupted())
    还是输出false,问什么???
      

  2.   

    (1)
    这是个线程自身中断的问题,当线程调用interrupt()时,有三种情况这个中断会起作用,
    1.当它处于wait()或join()中时,这时它会收到一个 InterruptedException. 这种是要try catch的.
    2.当它在一个interruptible channel 中阻塞时会收到一一个ClosedByInterruptException. 
    3.当它在一个selector 里阻塞时.
    如果这三种情况都不满足,那么这个线程只是简单的把设置为true,其他什么事都不干,继续运行,等到下面try catch时发现这个线程的中断状态为true,则catch语句块被执行,中断状态又被设置为法false,所以最后的输出又是发false了.
    总的来说,其实线程没有被中断,只是中断状态值被设置成了true.(2)
    不好意思,第二个问题在我机子上一点问题都没有,输出很正常:in run() - doing things after nap
    in run() - leaving normally
    in main() thread
    t.isInterrupted()false
    in main() - leaving
    Press any key to continue...
      

  3.   

    cat_871017(零下九度),你说的“3.当它在一个selector 里阻塞时.”怎么没有下文了?
      

  4.   

    public static boolean interrupted()Tests whether the current thread has been interrupted. The interrupted status of the thread is cleared by this method. In other words, if this method were to be called twice in succession, the second call would return false (unless the current thread were interrupted again, after the first call had cleared its interrupted status and before the second call had examined it). Returns:
    true if the current thread has been interrupted; false otherwise.
    See Also:
    isInterrupted()
      

  5.   

    sleep
    public static void sleep(long millis)
                      throws InterruptedExceptionCauses the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds. The thread does not lose ownership of any monitors. Parameters:
    millis - the length of time to sleep in milliseconds. 
    Throws: 
    InterruptedException - if another thread has interrupted the current thread. "The interrupted status of the current thread is cleared when this exception is thrown."
    See Also:
    Object.notify()
      

  6.   

    public class InterruptCheck extends Object {
    public static void main(String[] args) {
    // 取得main线程
    Thread t = Thread.currentThread();
    // false
    System.out.println("Point A: t.isInterrupted()=" + t.isInterrupted());
    // 仅设置该中断状态为true,见:api doc: public void interrupt()
    t.interrupt();
    // true
    System.out.println("Point B: t.isInterrupted()=" + t.isInterrupted());
    // true
    System.out.println("Point C: t.isInterrupted()=" + t.isInterrupted());try {
    Thread.sleep(2000);
    System.out.println("was NOT interrupted");
    } catch ( InterruptedException x ) {
    // The interrupted status of the current thread is cleared when this exception is thrown.
    System.out.println("was interrupted");
    }
    // false
    System.out.println("Point D: t.isInterrupted()=" + t.isInterrupted());
    }
    }