public class Main extends Thread {
@Override
public void run() {
while(!this.isInterrupted()){
System.out.println("the thread is run ...");
try {
TimeUnit.SECONDS.sleep(1L);
} catch (InterruptedException e) {
// ignore
}
}

}

public void goOn(){
Main.interrupted();
if(!this.isInterrupted()){
run();
}
}

public static void main(String[] args) throws InterruptedException{
Main td = new Main();
System.out.println("``````````````````````````````````````");
td.start();
TimeUnit.SECONDS.sleep(2L);
System.out.println("use interrupt()...");
td.interrupt(); // ...不好用?
TimeUnit.SECONDS.sleep(4L);
System.out.println("use goOn()...");
td.goOn();
}
}
有时候结果是:(预期)
`````````````````````````````````````
the thread is run ...
the thread is run ...
use interrupt()...
use goOn()...
the thread is run ...
the thread is run ...
the thread is run ...
有时候(非预期):
``````````````````````````````````````
the thread is run ...
the thread is run ...
use interrupt()...
the thread is run ...
the thread is run ...
the thread is run ...
the thread is run ...
use goOn()...
the thread is run ...
the thread is run ...
the thread is run ...
the thread is run ...
the thread is run ...jdk1.6_18

解决方案 »

  1.   

    System.out.println("use goOn()...");
    System.out.println("use interrupt()...");
    这二句话是属于主线程中的public void static mian()......这个方法是另外一个线程也就是程序的主线程你的程序一共有二个线程:主线程和你自己写的Main类线程..线程执行的顺序本来就是由jvm来调度的,没有一个定死的顺序,
    所以结果出来的并不会一样,,,
      

  2.   

    如果想都得到 (预期)结果,可以在while里加锁,中断前先获取锁
    不然结果没法预见.发送中断消息后要先获取中断锁 这时候while可能已被调度
      

  3.   

    2个线程没错,但输出
    the thread is run ...
    the thread is run ...
    use interrupt()...
    证明:
    1. run() 已经执行了.
    2.运行interrupt()后,线程应该处于中断状态,休息四秒..但问题是
    为预期情况
    the thread is run ...
    the thread is run ...
    use interrupt()...
    the thread is run ...
    the thread is run ...
    the thread is run ...
    没有出现等待...
      

  4.   

    我就是感觉  td.interrupt(); // ...不好用?
      

  5.   

    还有可能中断失败了
    * <p> If this thread is blocked in an invocation of the {@link
    * Object#wait() wait()}, {@link Object#wait(long) wait(long)}, or {@link
    * Object#wait(long, int) wait(long, int)} methods of the {@link Object}
    * class, or of the {@link #join()}, {@link #join(long)}, {@link
    * #join(long, int)}, {@link #sleep(long)}, or {@link #sleep(long, int)},
    * methods of this class, then its interrupt status will be cleared and it
    * will receive an {@link InterruptedException}.
    如果要中断的线程正在等待,中断标志会被清除并抛出异常,看一你的sleep是不是抛异常了 中断不成功
      

  6.   

    正解...发现问题了,问题出现在可能有时候TimeUnit.SECONDS.sleep(1L)和interrupt()同时运行了.嗯
    3Q
      

  7.   

    线程问题 本人PASS 完全不理解
      

  8.   

    这个interrupt(); 有问题。public class Main extends Thread {
    volatile boolean flag = false; @Override
    public void run() {
    while (!flag) {
    System.out.println("the thread is run ...");
    try {
    TimeUnit.SECONDS.sleep(1L);
    } catch (InterruptedException e) {
    // ignore
    }
    } } public void goOn() {
    if (flag) {
    flag = false;
    run();
    }
    } public static void main(String[] args) throws InterruptedException {
    Main td = new Main();
    System.out.println("``````````````````````````````````````");
    td.start();
    TimeUnit.SECONDS.sleep(2L);
    td.flag = true;
    System.out.println("use interrupt()...");
    TimeUnit.SECONDS.sleep(4L);
    System.out.println("use goOn()...");
    td.goOn();
    }
    }