Duplicate :
|
A licensee asked to clarify the specs. in Thread API and to mention in API document if it's specs. When a thread enters into sleep and an interruption happens to the thread, an InterruptedException occurs. (This is described in Thread#interrupt() API doc.) However, as the attached test case shows, sleep interval time is set to 0, interruption does not seem to occur. REPRODUCE : 1) Compile the following test case ===== Test Case ====== class SleepTest extends Thread { static int sleep_time; public static void main( String arg[] ) { try { sleep_time = Integer.parseInt( arg[0] ); SleepTest st = new SleepTest(); st.start(); Thread.sleep(1000); System.out.println("Interrupt..."); st.interrupt(); } catch( Exception e ) { e.printStackTrace(); } } public void run() { try { while( true ) { Thread.sleep(sleep_time); } } catch( InterruptedException e ) { e.printStackTrace(); } } } ======================= 2) Launch "java SleepTest <sleep interval time>" for example, if sleep interval is 2[sec], launch "java SleepTest 2000" Yo will see the message, Interrupt... java.lang.InterruptedException: sleep interrupted at java.lang.Thread.sleep(Native Method) at SleepTest.run(SleepTest.java:35) However, if you specify 0 as interval time java SleepTest 0 You can not see InterruptedException. REQUEST : The licensee requested to mention the above in API document like, "When an interruption happens to thread.sleep(0), InterruptedException does not appear." CONFIGURATION : OS : WindowsXP(SP1, Japanese) JRE : 1.4.2_06/5.0/6.0b11 ###@###.### 2004-11-12 02:44:33 GMT