Thread.isInterrupted总是返回false

时间:2013-12-19 09:19:43

标签: java linux multithreading

也许,这是Thread.isInterrupted doesn't work, Thread.interrupted does的重复问题。 但由于我使用一种完全不同的方式来调用Thread.isInterrupted()并升级到更新的jdk,我认为这可能会引起另一种问题。

我的程序中有三个线程(不包括主线程)。

  • 一个sleep线程休眠10秒;
  • checker线程检查sleep是否被另一个线程中断;
  • interrupter只要sleep线程启动就会中断它。

这是我的计划:

package foobar;

public class Foobar {

    public static void main(String[] args) throws Exception {
        Thread sleep = new Thread(new Sleep());
        sleep.start();

        Thread checker = new Thread(new InterruptedChecker(sleep));
        checker.start();

        Thread interrupter = new Thread(new Interrupter(sleep));
        interrupter.start();
    }
}

class InterruptedChecker implements Runnable {

    private final Thread thread;

    InterruptedChecker(Thread thread) {
        this.thread = thread;
    }

    @Override
    public void run() {
        while (true) {
            if (thread.isInterrupted()) {
                break;
            }
        }
        System.out.println("The thread is interrupted.");
    }
}

class Sleep implements Runnable {

    @Override
    public void run() {
        try {
            Thread.sleep(10 * 1000);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}

class Interrupter implements Runnable {

    private final Thread thread;

    Interrupter(Thread thread) {
        this.thread = thread;
    }

    @Override
    public void run() {
        System.out.println("interrupting...");
        thread.interrupt();
    }
}

有趣的是,程序一直在运行并且不会打印消息"The thread is interrupted.",这意味着sleep.isInterrupted()会返回false,但是它有些事

这是我的电脑的java -version(Ubuntu 13.10 64bit):

java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)

在我阅读上面的Thread.isInterrupted doesn't work, Thread.interrupted does后,我尝试使用较旧的jdk在不同的机器上编译我的代码并且它有效。

而且,这是机器的java -version(红帽企业Linux AS版本4(Nahant Update 3)64位):

java version "1.6.0_06"
Java(TM) SE Runtime Environment (build 1.6.0_06-b02)
Java HotSpot(TM) 64-Bit Server VM (build 10.0-b22, mixed mode)

所以我的问题是:

  1. 我的程序中有什么问题吗?
  2. 如果没有,多核cpu或64位系统是否与此问题有关?
  3. 修改

    感谢彼得的评论。

      

    你想解决什么问题?

    正如我在评论中所述,我刚刚阅读了the post,其中在“不要吞下中断”的章节中它说明了,

      

    调用堆栈上的代码可以了解中断并对其进行响应   如果它想。

    所以我不确定程序中的checker线程是否充当,这里称为什么,代码更高。我只是试着证明这个帖子是对的。但似乎有些不对劲。

2 个答案:

答案 0 :(得分:3)

中断标志在触发后立即复位。即,一旦sleep()被中断,该标志将被重置为false。


修改

  

所以我不确定我的程序中的检查程序线程是否充当,这里调用的是什么,代码更高。我只是试着证明这个帖子是对的。但似乎有些不对劲。

恕我直言,另一个线程没有更高的内容。你通常想到的是这样的事情。

public static void main(String... ignored) throws InterruptedException, ExecutionException {
    ExecutorService es = Executors.newSingleThreadExecutor();
    Future<?> future = es.submit(new Runnable() {
        @Override
        public void run() {
            methodA();
        }
    });
    Thread.sleep(1000);
    future.cancel(true); // interrupts task.
    long start = System.nanoTime();
    try {
        future.get();
    } catch (CancellationException expected) {
        // ignored
    }
    es.shutdown();
    es.awaitTermination(1, TimeUnit.MINUTES);
    long time = System.nanoTime() - start;
    System.out.printf("Time to cancel/shutdown was %.1f milli-seconds%n",
            time / 1e6);
}

private static void methodA() { // doesn't throw any checked exception
    for (int i = 0; i < 100; i++)
        methodB();
}

private static void methodB() {
    boolean conditionWhichIsTrue = true;
    if (conditionWhichIsTrue)
        try {
            Thread.sleep(500);
        } catch (InterruptedException ie) {
            Thread.currentThread().interrupt();
        }
}

打印

Time to cancel/shutdown was 1.7 milli-seconds

但是,如果你通过注释Thread.currentThread().interrupt();行吞下中断,这需要大约50秒才能停止,因为你只打断了一个睡眠呼叫。

答案 1 :(得分:0)

这个问题也困扰了我一段时间。经过一番调查后,我得到了答案。

InterruptedChecker检查isInterrupted状态时,Sleep线程已经死亡。尝试在Thread.currentThread().interrupt();课程中Sleep之后添加一些耗时的代码,然后重试。

<强>&#39;中断&#39;当该线程死亡时,线程的状态被清除。

相关问题