Uploaded image for project: 'Harmony'
  1. Harmony
  2. HARMONY-6653

Use instance lock to protect static share data in ObjID.java

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 6.0M1
    • Fix Version/s: 5.0M16
    • Component/s: Classlib
    • Labels:
      None
    • Environment:
      Window XP

      Description

      I found unsafe synchonrization in ObjID.java as follow:

      private static class NumberGenerator {
      static long numCounter = 65536;
      ...
      synchronized long nextLong()

      { return useRandom ? sGenerator.nextLong() : numCounter++; }

      ...
      }

      numCounter is a static class field. querySerialNumber increment (non-atomic) operation is in a synchronized method which means it is protected by "this".

      Consequence:
      Different object instances will synchronize on different "this", numCounter increment is not protected by a uniform lock. It results in race condition in numCounter increment, thus having incorrect numCounter value.

      It would be more proper to write the code as follows:
      private static class NumberGenerator {
      static long numCounter = 65536;
      private static final Object monitor = new Object()
      ...
      long nextLong() {
      synchornized(monitor)

      { return useRandom ? sGenerator.nextLong() : numCounter++; }

      }
      ...
      }

        Attachments

          Activity

            People

            • Assignee:
              tellison Tim Ellison
              Reporter:
              cashcrop Wendy Feng
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Time Tracking

                Estimated:
                Original Estimate - 2h
                2h
                Remaining:
                Remaining Estimate - 2h
                2h
                Logged:
                Time Spent - Not Specified
                Not Specified