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

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

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P4
    • Resolution: Fixed
    • Affects Version/s: 1.4.1, 1.4.2
    • Fix Version/s: 6
    • Component/s: core-libs
    • Subcomponent:
    • Resolved In Build:
      b27
    • CPU:
      generic, x86
    • OS:
      generic, linux

      Description



      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;
          }

      }

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

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: