JDK-4422466 : IllegalThreadStateException: Dead Thread not thrown in 1.3.0
  • Type: Bug
  • Component: hotspot
  • Sub-Component: runtime
  • Affected Version: 1.3.0,1.3.1,1.4.1
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic,linux,windows_2000
  • CPU: x86
  • Submitted: 2001-03-07
  • Updated: 2003-04-25
  • Resolved: 2003-04-25
Related Reports
Duplicate :  
Description

Name: boT120536			Date: 03/06/2001


java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0)
Java HotSpot(TM) Client VM (build 1.3.0, mixed mode)

1.3.0 runtime does not throw java.lang.IllegalThreadStateException: Dead Thread
exception on attempted thread reuse. I had been previously unaware that thread
reuse was prohibited. The java.lang.Thread javadoc gives no indication of this
thread use limitation. The only way I was able to discover the actual source of
this problem was by running this test program on 1.2.1.

public class ReuseThread extends Thread
{
  public void run()
  {
    System.err.println("enter run()");
    try {
      while (!isInterrupted()) {
        sleep(1000);
      }
    } catch (InterruptedException e) {}
    System.err.println("leave run()");
  }

  public static void main(String[] args) throws Exception
  {
    Thread reuseThread = new ReuseThread();
    System.err.println("starting thread");
    reuseThread.start();
    Thread.sleep(1000);
    System.err.println("interrupting thread");
    reuseThread.interrupt();
    Thread.sleep(1000);
    System.err.println("joining thread");
    reuseThread.join();
    Thread.sleep(1000);
    System.err.println("starting thread");
    reuseThread.start();
    Thread.sleep(5000);
  }
}

1.3.0 output:
starting thread
enter run()
interrupting thread
leave run()
joining thread
starting thread

1.2.1 output:
starting thread
enter run()
interrupting thread
leave run()
joining thread
starting thread
Exception in thread "main" java.lang.IllegalThreadStateException: Dead Thread
        at java.lang.Thread.start(Native Method)
        at ReuseThread.main(ReuseThread.java:29)
(Review ID: 118232) 
======================================================================

Name: rmT116609			Date: 08/16/2001


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)

The following code creates a thread and start it tree times. When this code
is executed with JDK 1.2.2 Classic VM (build Linux_JDK_1.2.2_FCS, native
threads, sunwjit), the following exception is produced:

Exception in thread "main" java.lang.IllegalThreadStateException
        at java.lang.Thread.start(Native Method)
        at Test.main(Test.java, Compiled Code)

But when is executed with HotSpot Client VM (build 1.3.1-b24, mixed mode),
exception is not produced.

class Test extends Thread
{
  public void run () {}

  public static void main (String argv [])
  {
    Test t = new Test ();

    for (int i = 0; i < 3; i++)
    {
      t.start ();

      try
      {
        t.join ();
      }
      catch (Exception e) {}
    }
  }
}
(Review ID: 130146)
======================================================================

Name: nt126004			Date: 01/28/2003


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

FULL OPERATING SYSTEM VERSION :
Microsoft Windows 2000 [Version 5.00.2195]

A DESCRIPTION OF THE PROBLEM :
After allocating a Thread object, invoking start(), and
wating for run() to exit, a subsequent invocation of start
() should throw IllegalThreadStateException.  It does
nothing.
Also, documentation should be enhanced to explicitly state
that the start() method of a Thread object can be invoked
once and only once (if there is a valid reason for this
behaviour).  Otherwise, why not allow the start() method to
be repeatedly invoked assuming run() has exited and isAlive
() returns false?


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Run the following source code


EXPECTED VERSUS ACTUAL BEHAVIOR :
The results of running the above are absolutely nothing.
No exception is thrown when thread.start() is invoked the
second time but neither is run() invoked.
   From previous bug reports, it seems as though invoking
start() the second time should throw an
IllegalThreadStateException.  However, the API
documentation seems vague on this.  I expected that as long
as isAlive() returns false, it is safe to invoke start().
Apparently, Threads have another, undocumented, state
of 'exhausted' or similar and while in this state isAlive()
is false but invoking start() will fail.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
public class Test
{
      private Thread thread = new Thread()
      {
            public void run()
            {
                  for( int i=0; i<10; i++ )
                        System.out.println("In thread.run() " + i);
            }
      };

      public Test()
      {
      }

      public void execute()
      {
            System.out.println("execute()");
            thread.start();

            try {
                  //wait for run() to exit
                  Thread.sleep(10*1000);
            }
            catch (InterruptedException ex) {
            }

            if( thread.isAlive()==false )
                  thread.start();

            try {
                  //wait
                  Thread.sleep(10*1000);
            }
            catch (InterruptedException ex) {
            }
      }

      public static void main(String[] args)
      {
            Test test = new Test();
            test.execute();
      }
}

---------- END SOURCE ----------

CUSTOMER WORKAROUND :
Allocate the Thread object just before invoking start() and
then discard it.  Also, instead of extending the Thread
class, creating a class that implements Runnable works.
Either way, every invocation of start() requires a 'fresh'
Thread object.
(Review ID: 180439)
======================================================================

Comments
WORK AROUND Name: boT120536 Date: 03/06/2001 Don't attempt to reuse threads. This doesn't work, even though there is no exception thrown and no generally available documentation that this won't work. ======================================================================
11-06-2004

EVALUATION This bug is a duplicate of 4773384 1.4 doesn't throw IllegalThreadStateException and will be tracked under that bug number. 4773384 has been committed to Tiger. ###@###.### 2003-04-25 -----------------------------------------------
25-04-2003