Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-1258606

Thread.interrupt() is ignored during sleep()

XMLWordPrintable

    • 1.1
    • sparc
    • solaris_2.5
    • Not verified

      The thread.interrupt() method does not interrupt a sleep() call.

      This test case takes 10 seconds to run. Run 'java InterruptTest'

      /*
       * InterruptTest.java
       */

      class InterruptMeThread extends Thread
      {
          public void run ()
          {
      try
      {
      System.out.println("Thread 2 sleeping... " + InterruptTest.elapsed());
      //sleep(Long.MAX_VALUE); // ERROR - RETURNS IMMEDIATELY
      sleep(10000); // works
      System.out.println("Thread 2 done sleeping " + InterruptTest.elapsed());
      }
      catch(InterruptedException e)
      {
      System.out.println("Thread 2 interrupted!! "+ InterruptTest.elapsed());;
      }
      System.out.println("Thread 2 exiting "+ InterruptTest.elapsed());
          }
      }

      public class InterruptTest extends Thread
      {
          public static long startMs = 0;

          public static void main (String args[])
          {
      startMs = System.currentTimeMillis();
      Thread thread = new InterruptMeThread();
      thread.start();
      try{Thread.sleep(3000);} catch (Exception e) {}
      System.out.println("Thread 1 interrupting Thread 2 "+ InterruptTest.elapsed());;
      thread.interrupt();
      System.out.println("Thread 1 exiting "+ InterruptTest.elapsed());;
          }

          public static String elapsed()
          {
      return "[" + ((System.currentTimeMillis() - startMs)/1000) + "s]";
          }
      }


            tlindholsunw Timothy Lindholm (Inactive)
            jdunnsunw Jeffrey Dunn (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: