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

(thread) If main thread dies from unhandled exception, isAlive() is still true

    Details

    • Subcomponent:
    • Introduced In Build:
      1.1
    • Introduced In Version:
      1.1
    • Resolved In Build:
      b44
    • CPU:
      x86
    • OS:
      windows_2000

      Description



      Name: nt126004 Date: 03/14/2003


      FULL PRODUCT VERSION :
      java version "1.3.1"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-b24)
      Java HotSpot(TM) Client VM (build 1.3.1-b24, mixed mode)

      (Also tested with 1.4.1 on Windows and Linux)

      FULL OS VERSION :
      Microsoft Windows 2000 [Version 5.00.2195]

      A DESCRIPTION OF THE PROBLEM :
      If a thread uses join() to wait for the main thread to terminate, but the main thread exits due to an unhandled exception, the join() does not complete. If join(n) is used, at the end of the timeout period the main thread is reported as alive by isAlive(). The join() terminates normally if the main thread exits normally (i.e. not as the result of an unhandled exception). For threads other than the main thread, join() and isAlive() work as expected.

      The outcome is that an application which generates an unhandled exception in the main thread will never terminate if another thread is using isAlive() and/or join() to wait for the main thread to die. The monitoring thread stays alive forever waiting for the (now moribund) main thread to finish, which it never does.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile and run the code below.

      Also: comment out the "throw" statement in main to see it working normally in the absence of an unhandled exception. Or, comment out the line "w.start()" near the beginning of main() to see it working normally if an unhandled exception occurs but no other thread is waiting for the main thread to terminate.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      Waiting for Thread[main,5,main]
      Joined Thread[main,5,main]
      Waiting for Thread[main,5,main]
      java.lang.RuntimeException: Bang!
      at Termination.main(Termination.java:9)
      Joined Thread[main,5,main]
      Terminated: Thread[main,5,main]
      Waiting for Thread[main,5,main]
      Joined Thread[main,5,main]
      Waiting for Thread[main,5,main]
      java.lang.RuntimeException: Bang!
      at Termination.main(Termination.java:9)
      Joined Thread[main,5,main]
      Waiting for Thread[main,5,main]
      Joined Thread[main,5,main]
      Waiting for Thread[main,5,main]
      Joined Thread[main,5,main]
      Waiting for Thread[main,5,main]
      Joined Thread[main,5,main]
      Waiting for Thread[main,5,main]
      Joined Thread[main,5,main]
      Waiting for Thread[main,5,main]
      [...and so on...]

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class Termination {
        public static void main (String args[]) {
          Waiter w = new Waiter();
          w.start();
          try {
            Thread.currentThread().sleep(1000);
          }
          catch (InterruptedException e) { }
          throw new RuntimeException("Bang!"); // the thread is still
        } // alive after this???
      }

      class Waiter extends Thread {
        Thread t;
        public Waiter () {
          t = Thread.currentThread();
        }
        public void run () {
          while (t.isAlive()) {
            System.out.println("Waiting for " + t);
            try {
              t.join(500);
            }
            catch (InterruptedException e) {
              System.out.println("Interrupted");
            }
            System.out.println("Joined " + t);
          }
          System.out.println("Terminated: " + t);
        }
      }
      ---------- END SOURCE ----------
      (Review ID: 182413)
      ======================================================================

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                martin Martin Buchholz
                Reporter:
                nthompsosunw Nathanael Thompson (Inactive)
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: