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

Process.exitValue() & Thread.activeCount() incorrect on Linux

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 1.3.0
    • core-libs



      Name: rmT116609 Date: 12/19/2000


      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)


      On Linux only, (is OK on Windows NT): If a thread creates a Process object by
      using Runtime.getRuntime().exec(...) and that thread finishes before the
      process that it created then Process.exitValue() and Thread.activeCount() do
      not behave correctly when the new process eventually does die.

      Process.exitValue() does not detect the termination of the sub-process in the
      case described above. Thread.activeCount() is one higher than it should be in
      the case described above.

      Here is the exact code to reproduce the problem on Linux (OK on windows NT:)


      import java.io.*;
      import java.math.*;

      public class Main1
      {
        class MonitorThread extends Thread
        {
          Main1 main1 = null;

          MonitorThread(Main1 main1)
          {
            this.main1 = main1;
          }

          public void run()
          {
            try
            {
              while ( true )
              {
                Thread.currentThread().sleep(1000);
                synchronized ( main1 )
                {
                  System.out.println("Seeing if sub process has died...");
                  try
                  {
                    if ( main1.process != null )
                    {
                      main1.process.exitValue();
                      System.out.println("Sub process has died!");
                      break;
                    }
                  }
                  catch (Exception exception)
                  {
                    System.out.println("Sub process has NOT died yet!");
                  }
                }
              }
            }
            catch (Exception exception)
            {
              exception.printStackTrace();
            }
          }
        }

        class MakeProcessThread extends Thread
        {
          Main1 main1 = null;

          MakeProcessThread(Main1 main1)
          {
            this.main1 = main1;
          }

          public void run()
          {
            try
            {
              System.out.println("Starting sub process...");
              synchronized ( main1 ) {
      // main1.process = Runtime.getRuntime().exec("java -cp . Main2");
                main1.process = Runtime.getRuntime().exec("java -cp e:\\data\\JBuilder4\\test2\\classes Main2");
              }

              /* If we ensure that this thread does not terminate before the
                 sub-process that this process created terminates (i.e. by sleeping
                 long enough) then the monitor thread correctly detects the
                 termination of the subprocess. */

              Thread.currentThread().sleep(10000);

              /* However if this thread terminates before the subprocess it created
                 then the monitor thread does NOT detect the termination of the
                 subprocess. */

              /* This does NOT happen on Windows NT, just Linux */
            }
            catch (Exception exception)
            {
              exception.printStackTrace();
            }
          }
        }

        public static void main(String[] args)
        {
          try
          {
            Main1 main1 = new Main1();
          }
          catch (Exception exception)
          {
            exception.printStackTrace();
          }
        }

        private Process process = null;

        public Main1()
        {
          try
          {
            Thread monitorThread = new MonitorThread(this);
            monitorThread.start();
            Thread makeProcessThread = new MakeProcessThread(this);
            makeProcessThread.start();

            monitorThread.join();
            makeProcessThread.join();

            /* Also activeCount is now 2, but it should be 1 */
            int activeCount = 0;
            while ( (activeCount=Thread.activeCount()) != 1 )
            {
              System.out.println("activeCount="+activeCount);
              Thread.currentThread().sleep(1000);
            }
            System.out.println("Process finished.");
          }
          catch (Exception exception)
          {
            exception.printStackTrace();
          }
        }
      }


      public class Main2
      {
        static public void main(String[] args)
        {
          try
          {
            Thread.currentThread().sleep(5000);
          }
          catch (Exception exception)
          {
            exception.printStackTrace();
          }
        }
      }


      With thanks,

      Adrian.
      (Review ID: 113929)
      ======================================================================

            Unassigned Unassigned
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: