Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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();
}
like image 377
ripper234 Avatar asked Jan 06 '10 10:01

ripper234


1 Answers

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.

like image 158
Mocky Avatar answered Nov 14 '22 06:11

Mocky