Duplicate :
|
|
Duplicate :
|
|
Relates :
|
Name: rlT66838 Date: 04/10/2000 java version "1.3.0rc2" Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0rc2-Y) An interrupted thread appears not to be able to produce output through System.err and System.out. This remains the case while the 'interrupted' flag is set. Once the 'interrupted' flag has been cleared (either by a call to Thread.interrupted or to an 'interruptable' method such as Thread.sleep), the thread produces output normally. Below is a Java class that demonstrates the problem.'java InterruptBug yes' runs a thread which is (immediately) interrupted. 'java InterruptBug no' runs a thread which is not interrupted. In both cases the thread repeatedly (once every second, for five seconds) checks and prints the outcome of 'isInterrupted()'. After that, it does the same for 'interrupted()'. The 'no' version will produce correct output during the full ten seconds. However the 'yes' version will buffer its output during the first five or six seconds. Then, after clearing the 'interrupted' flag, the buffered output is printed, followed by the remaining output. Output of the 'no version': bash-2.00$ java InterruptBug no not interrupting isInterrupted: false isInterrupted: false isInterrupted: false isInterrupted: false isInterrupted: false interrupted: false interrupted: false interrupted: false interrupted: false interrupted: false Output of the 'yes' version: bash-2.00$ java InterruptBug yes interrupting isInterrupted: true isInterrupted: true isInterrupted: true isInterrupted: true isInterrupted: true interrupted: true interrupted: false interrupted: false interrupted: false interrupted: false Note that the 'yes' output shown above is correct. However there is a delay, which is incorrect. public class InterruptBug implements Runnable { public static void main (String[] args) { if (args.length != 1) { System.err.println ("Usage: InterruptBug [yes|no]"); return; } boolean do_interrupt = "yes".equals (args [0]); Thread t = new Thread (new InterruptBug()); t.start(); if (do_interrupt) { System.out.println ("interrupting"); t.interrupt(); } else System.out.println ("not interrupting"); } public void run() { // for five seconds, repeatedly check the 'interrupted' flag for (int j = 0; j < 5; j++) { sleep (1000); if (Thread.currentThread().isInterrupted()) System.out.println ("isInterrupted: true"); else System.out.println ("isInterrupted: false"); } // for five seconds, repeatedly check and clear the 'interrupted' flag for (int j = 0; j < 5; j++) { sleep (1000); if (Thread.interrupted()) System.out.println ("interrupted: true"); else System.out.println ("interrupted: false"); } } /** Sleep without using Thread.sleep. */ private static void sleep (long millis) { long end_time = System.currentTimeMillis() + millis; while (System.currentTimeMillis() < end_time) ; } } (Review ID: 103479) ====================================================================== Name: yy116575 Date: 02/15/2001 Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0) Java HotSpot(TM) Client VM (build 1.3.0, mixed mode) Calling System.out.println clears the interrupted flag for the thread. The following example code has the following output: Before => true After => false public class junk { public static void main(String[] args) { try { Thread.currentThread().interrupt(); boolean keepInterrupted = Thread.currentThread().isInterrupted(); System.out.println(); // This blank line disappears. System.out.println("Before => " + keepInterrupted); System.out.println("After => " + Thread.currentThread().isInterrupted()); } catch (Exception e) { e.printStackTrace(); } } } (Review ID: 117079) ====================================================================== In the first test case, if the output is flushed then it appears immediately but that seems to clear the interrupted status: > java InterruptBug yes interrupting isInterrupted: true isInterrupted: false isInterrupted: false isInterrupted: false isInterrupted: false interrupted: false interrupted: false interrupted: false interrupted: false interrupted: false This doesn't occur on Linux. michael.mccloskey@eng 2001-04-03
|