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

java.lang.Thread.sleep(timeout) hangs forever.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P2 P2
    • None
    • 1.2.0
    • hotspot
    • sparc
    • solaris_2.4



      Name: akC45999 Date: 01/22/98



      Specification of class java.lang.Thread reads:

        public static void sleep(long millis) throws InterruptedException
           Causes the currently executing thread to sleep (temporarily cease
      execution) for the specified number of milliseconds.

      But Sun's JDK 1.0.2 ... 1.2 never wakes up the thread under some circumstanses.

      The test below optionally starts 2 child threads: a lazy thread which sleeps for
      some time t2 and exits, and a busy thread which does some computational work.
      After starting the child threads, the main thread sleeps for some time t1.

      When both threads are started, the main thread sleeps for t1+t2,
      and when only busy thread started, the main thread sleeps forever.

      ------------------------------------- file lazyThreads.java
      import java.io.PrintStream;

      public class lazyThreads extends Thread {
        static int t1=1000, t2=1500;
        static PrintStream out=System.out;
        static volatile boolean finish=false;
        boolean kind;

        void busy() { // do some silly stuff
      float sum=0; int k=1;
      while (!finish) {
      sum=sum+1/k;
      }
        }

        void lazy() { // wait for t2
      long time0=System.currentTimeMillis();
      try{
      Thread.sleep(t2);
      out.println("lazy: waked up in "+(System.currentTimeMillis()-time0));
      } catch (InterruptedException e) {
      out.println("lazy: InterruptedException");
      }
        }

        lazyThreads(boolean kind) {
      this.kind=kind;
        }

        public void run() {
      if (kind) busy(); else lazy();
        }


        public static void main(String[] args) {//throws Exception {
      boolean startLazy=false, startBusy=false;
      for (int k=0; k<args.length; k++) {
      if (args[k].startsWith("l")) startLazy=true;
      if (args[k].startsWith("b")) startBusy=true;
      }

      Thread lazyThread = new lazyThreads(false);
      if (startLazy) lazyThread.start();

      Thread busyThread = new lazyThreads(true);
      if (startBusy) busyThread.start();

      long time0=System.currentTimeMillis();
      try{
      Thread.sleep(t1);
      out.println("ending in "+(System.currentTimeMillis()-time0)+" msec");
      } catch (InterruptedException e) {
      out.println("Main Thread:InterruptedException");
      }
      finish=true;
        }

      }
      ------------------------------------- end of file lazyThreads.java

      Running the test:
      novo64% javac lazyThreads.java
      novo64% setenv CLASSPATH .
      novo64% java lazyThreads
      ending in 1001 msec
      novo64% java lazyThreads l
      ending in 1005 msec
      lazy: waked up in 1505

      So far, so good - results as expected. But when both lazy and busy threads
      are started, both thread sleeps for t1+t2 ms:

      novo64% java lazyThreads b l
      ending in 1506 msec
      lazy: waked up in 1538

      Finally, if only busy thread is started, both threads sleep forever,
      and cntrlC has to be used:

      novo64% java lazyThreads b
      ^Cnovo64%

      ======================================================================

            sliangsunw Sheng Liang (Inactive)
            rfqsunw Rfq Rfq (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: