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

java.io.Print{Stream,Writer}.println(Object) oversynchronized, can deadlock

XMLWordPrintable

    • b27
    • generic, x86
    • generic, linux



      Name: dk106046 Date: 08/13/2003

      java version "1.4.2-beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-beta-b06)
      Java HotSpot(TM) Client VM (build 1.4.2-beta-b06, mixed mode)

      Compile and execute this multi threaded testcase.

      Ex: java Test 5

      You can see the testcase hanging. Press Ctrl+\ to get the Javacore and see a dead lock detected between Object TestObj and java.io.PrintStream.

      Test.java


      public class Test extends Thread
      {
          private static TestObj testObj = new TestObj("This is a test.");
          private static int loopNum = 100;

          public void run()
          {
              for(int i=0; i<loopNum; i++)
              {
                  testObj.test();
                  //passing an object to System.out.println might cause deadlock
                  //if the object has a synchronized toString() method.
                  //using System.out.println(testObj.toString()) won't have a problem.

                 System.out.println(testObj);

              }
          }

          public static void main(String args[])
          {
              if (args.length>2 || args.length<1)
              {
                  System.out.println("Usage: Test thread_number [loop_number]");
                  System.exit(1);
              }

              int num = new Integer(args[0]).intValue();
              if(args.length>1)
                  loopNum = new Integer(args[1]).intValue();

              Test[] t = new Test[num];
              for(int i=0; i<num; i++)
              {
                  t[i] = new Test();
                  t[i].start();
              }

              try
              {

                  for(int i=0; i <num; i++)
                      t[i].join();
              }
              catch (InterruptedException e)
              {
                  e.printStackTrace();
              }
              System.out.println("Test completed");
          }
      }

      class TestObj
      {
          String mStr;

          TestObj(String str)
          {
              mStr = str;
          }

          synchronized void test()
          {
              try
              {
                  long t = Math.round(Math.random()*10);
                  Thread.currentThread().sleep(t);
              }
              catch (InterruptedException e)
              {
                  e.printStackTrace();
              }

              //the following line might cause hang if there is System.out.println(testObj)
              //called by other threads.
              System.out.println("In test().");
          }

          synchronized public String toString()
          {
                 System.out.println("Calling toString\n");
              return mStr;
          }

      }

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

            sjiang Shanliang Jiang (Inactive)
            dkorbel David Korbel (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: