Thread.isInterrupted doesn't work, Thread.interrupted does

拥有回忆 提交于 2019-12-10 12:50:29

问题


The following program demonstrates the problem (latest JVM & whatnot):

public static void main(String[] args) throws InterruptedException {
    // if this is true, both interrupted and isInterrupted work
    final boolean withPrint = false;

    // decide whether to use isInterrupted or interrupted.
    // if this is true, the program never terminates.
    final boolean useIsInterrupted = true;

    ExecutorService executor = Executors.newSingleThreadExecutor();
    final CountDownLatch latch = new CountDownLatch(1);
    Callable<Void> callable = new Callable<Void>() {
        @Override
        public Void call() throws Exception {
            Random random = new Random();
            while (true) {
                if (withPrint) {
                    System.out.println(random.nextInt());
                    System.out.flush();
                }
                if (useIsInterrupted)
                {
                    if (Thread.currentThread().isInterrupted())
                        break;
                }
                else
                {
                    if (Thread.interrupted())
                        break;
                }
            }
            System.out.println("Nice shutdown");
            latch.countDown();
            return null;
        }
    };
    System.out.println("Task submitted");
    Future<Void> task = executor.submit(callable);
    Thread.sleep(100);
    task.cancel(true);
    latch.await();
    System.out.println("Main exited");
    executor.shutdown();
}

回答1:


This looks like a known issue with multi-processor machines, mainly in 64-bit OS and java versions from 1.5 - 7.0

A DESCRIPTION OF THE PROBLEM : While running two simultaneous threads, the first thread interrupts the second one using Thread.interrupt(). The second thread tests if it has been interrupted calling the Thread.isInterrupted() method which always returns false.

This occurs on a multiprocessor PC running a 64-Bit OS (Vista and Linux). On Vista 64-Bit, this occurs when using a 64-bit JVM (all versions from 1.5 to 1.7), but does not occur when using a 32-bit JVM. On Linux 64-bit, this occurs when using a 64-bit JVM (all versions from 1.5 to 1.7) or when using a 32-bit JVM (all versions from 1.5 to 1.7).

The solution is to install the version with the fix, which is 1.6.0_16-b02 or later.




回答2:


ripper234, i just ran this on my machine and it always stops no matter what value i use for the prints and which interrupts to use. I am using jdk1.6.0_16. looking at the javadoc, maybe it has something to do with the fact that interrupted() clears the (interrupted) state after each call and isInterrupted() does not. the fact that it works sometimes for jerome, always for me, and never(?) for you could indicated a difference in the jdks we are using OR the speed of our machines. if it has anything to do with the clearing of the state, that might explain the variability.



来源:https://stackoverflow.com/questions/2012259/thread-isinterrupted-doesnt-work-thread-interrupted-does

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!