1.
interrupt函数应当就是对相应的线程置中断标志位,在一个线程的run()方法中我认为不是时时刻刻都在监视这个中断标志位的;
当线程运行到sleep的时候,会检查这个中断标志位,如果是中断,那么就抛出异常;
所以我认为当你的线程处于运行状态的时候,就是没有运行到sleep的时候,即使被interrupt,这个线程也不会有反应,一直到执行到sleep为止。sleep执行后,中断标志被赋值为false。
你可以做这样的例子:
假设有一个thread:A,在A.start()之前,调用A.interrupt();之后在start()
A中的run()
{
   System.out.println("enter thread");
   try{
      Thread.sleep(100);
   }
   catch(......)
   {
      System.out.println("interrupt");
   }
   System.out.println("leave thread");
}看看你的输出是什么样的?
我想应当是
enter thread
interrupt刚开始看线程不到一周,就是这么理解的,请大家多讨论,多指正!!!!

解决方案 »

  1.   

    再看看这个例子
    public class PendingInterrupt extends Object {
    public static void main(String[] args) {
    if ( args.length > 0 ) {
    Thread.currentThread().interrupt();
    }  long startTime = System.currentTimeMillis();
    try {
    Thread.sleep(2000);
    System.out.println("was NOT interrupted");
    } catch ( InterruptedException x ) {
    System.out.println("was interrupted");
    } System.out.println(
    "elapsedTime=" + ( System.currentTimeMillis() - startTime ));
    }
    }