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

All static methods are treated as synchronized, whether they are or not

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 1.1
    • hotspot
    • None
    • x86
    • windows_95



      Name: mc57594 Date: 02/07/97
      Please notice that there are TWO sections of code submitted. I included
      both here for completeness.
      -mark

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

      Under win32, all static methods are getting a monitor lock rather than just those that are explicitly mark synchronized. The following program gives this output before hanging (correctly) under JDK 1.0.2:

        Should print 'hello'.
        Hello.
        It worked. Program should now hang.

      And this output before hanging (incorrectly) under JDK 1.13b:

        Should print 'hello'.

      This is not affected by what compiler is used to compile the program, only what VM it is run under.

      // file: testlock.java

      public class testlock
      {
         synchronized static public void shouldHang()
         {
            System.out.println("Didn't hang, it should've.");
         }

         static public void shouldNotHang()
         {
            System.out.println("Hello.");
         }

         static public void main(String args[])
         {
            testlock test = new testlock();
            synchronized (test.getClass())
            {
               new Thread(new trylock()).start();
            }
            System.out.println("Exiting.");
         }
      }

      class trylock implements Runnable
      {
         public void run()
         {
            System.out.println("Should print 'hello'.");
            testlock.shouldNotHang();
            System.out.println("It worked. Program should now hang.");
            testlock.shouldHang();
         }
      }

      The relevant portion of the trace follows:

      Full thread dump:
          "Thread-1" (TID:0xe63098, sys_thread_t:0x77b300, Win32ID:0xfffd83c7, state:MW) prio=5
              trylock.run(testlock.java:31)
              java.lang.Thread.run(Thread.java:336)
          "Finalizer thread" (TID:0xe60180, sys_thread_t:0x774e80, Win32ID:0xfffd974f, state:CW) prio=2
          "main" (TID:0xe60150, sys_thread_t:0x77d8a0, Win32ID:0xfffdadb3, state:R) prio=5
              java.lang.Thread.suspend(Thread.java:443)
              testlock.main(testlock.java:21)
      Monitor Cache Dump:
               java.lang.Class@E63078/EAC530 (key=0xe63078): "main"
               java.lang.Class@E630A0/EAC5F0 (key=0xe630a0): "Thread-1"

      company - 601 Inc. , email - ###@###.###
      ======================================================================
      From: ###@###.###

      Synopsis: Corrected bug report: All static methods treated as if synchronized.
      Severity Impact: (internal)
      Severity Functionality: (internal)
      Priority: (internal)
      Description: My previous bug report omitted a line of code crucial to reproducing the problem. Here is the corrected code (note the suspend() call to ensure that the lock is maintained):

      public class testlock
      {
         synchronized static public void shouldHang()
         {
            System.out.println("Didn't hang, it should've.");
         }

         static public void shouldNotHang()
         {
            System.out.println("Hello.");
         }

         static public void main(String args[])
         {
            testlock test = new testlock();
            synchronized (test.getClass())
            {
               new Thread(new trylock()).start();
               Thread.currentThread().suspend(); // forgot this line
            }
            System.out.println("Exiting.");
         }
      }

      class trylock implements Runnable
      {
         public void run()
         {
            System.out.println("Should print 'hello'.");
            testlock.shouldNotHang();
            System.out.println("It worked. Program should now hang.");
            testlock.shouldHang();
         }
      }

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

            sliangsunw Sheng Liang (Inactive)
            mchamnessunw Mark Chamness (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: