Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Thread.isInterrupted始终返回false_Java_Linux_Multithreading - Fatal编程技术网

Java Thread.isInterrupted始终返回false

Java Thread.isInterrupted始终返回false,java,linux,multithreading,Java,Linux,Multithreading,也许,这是一个重复的问题。 但是,由于我使用了一种完全不同的方式来调用Thread.isInterrupted(),并且已经升级到了一个新的jdk,我认为这可能是另一种问题造成的 我的程序中有三个线程(不包括主线程) 睡眠线程睡眠10秒 检查器线程检查睡眠是否被另一个线程中断 中断程序会在线程启动时中断睡眠线程 这是我的节目: package foobar; public class Foobar { public static void main(String[] args)

也许,这是一个重复的问题。 但是,由于我使用了一种完全不同的方式来调用
Thread.isInterrupted()
,并且已经升级到了一个新的jdk,我认为这可能是另一种问题造成的

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

  • 睡眠
    线程睡眠10秒
  • 检查器
    线程检查
    睡眠
    是否被另一个线程中断
  • 中断程序
    会在线程启动时中断
    睡眠
    线程
这是我的节目:

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();
    }
}
有趣的是,有些事情(相当多)程序一直在运行,并且没有打印消息
“线程被中断。”
,这意味着
sleep.isInterrupted()
返回
false
,但它确实返回
一些事情

以下是我的电脑(Ubuntu 13.10 64位)的java版本:

读了上面的内容后,我尝试在另一台机器上使用较旧的jdk编译代码,结果成功了

下面是该机器的
java-version
(Red Hat Enterprise Linux AS release 4(Nahant Update 3)64位):

所以我的问题是:

  • 我的程序有问题吗
  • 如果没有,多核cpu或64位系统是否与此问题有关
  • 编辑 谢谢彼得的评论

    你想解决什么问题

    正如我在评论中所说,我刚刚读到,的章节“不要吞咽中断”中说

    调用堆栈上层的代码可以了解中断并对其作出响应 如果它愿意的话


    因此,我不确定我的程序中的
    检查器
    线程是否充当(这里称之为)更高级的代码。我只是想证明这篇文章是对的。但似乎出了什么问题。

    中断标志一经触发就会重置。i、 e.一旦sleep()被中断,该标志将重置为false


    编辑

    因此,我不确定我的程序中的checker线程是否充当更高的代码。我只是想证明这篇文章是对的。但似乎有什么不对劲

    我觉得另一条线没有更高的层次。你通常的想法是这样的

    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();
            }
    }
    

    但是,如果您通过注释掉
    Thread.currentThread().interrupt()来接受中断行由于您只中断了一次睡眠呼叫,因此停止此操作大约需要50秒。

    中断标志一经触发就会重置。i、 e.一旦sleep()被中断,该标志将重置为false


    编辑

    因此,我不确定我的程序中的checker线程是否充当更高的代码。我只是想证明这篇文章是对的。但似乎有什么不对劲

    我觉得另一条线没有更高的层次。你通常的想法是这样的

    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();
            }
    }
    

    但是,如果您通过注释掉
    Thread.currentThread().interrupt()来接受中断行由于您只中断了一次睡眠呼叫,因此停止此操作大约需要50秒。

    中断标志一经触发就会重置。i、 e.一旦sleep()被中断,该标志将重置为false


    编辑

    因此,我不确定我的程序中的checker线程是否充当更高的代码。我只是想证明这篇文章是对的。但似乎有什么不对劲

    我觉得另一条线没有更高的层次。你通常的想法是这样的

    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();
            }
    }
    

    但是,如果您通过注释掉
    Thread.currentThread().interrupt()来接受中断行由于您只中断了一次睡眠呼叫,因此停止此操作大约需要50秒。

    中断标志一经触发就会重置。i、 e.一旦sleep()被中断,该标志将重置为false


    编辑

    因此,我不确定我的程序中的checker线程是否充当更高的代码。我只是想证明这篇文章是对的。但似乎有什么不对劲

    我觉得另一条线没有更高的层次。你通常的想法是这样的

    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();
            }
    }
    

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

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

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


    当线程死亡时,线程的“中断”状态被清除。这个问题也困扰了我一段时间。经过调查,我得到了答案

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


    当线程死亡时,线程的“中断”状态被清除。这个问题也困扰了我一段时间。经过调查,我得到了答案

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

    线程的“中断”状态已清除