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

java.lang.Class.getAnnotations() always enters synchronized method

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P3
    • Resolution: Fixed
    • Affects Version/s: 6u31
    • Fix Version/s: 8
    • Component/s: core-libs
    • Labels:
      None
    • Subcomponent:
    • Introduced In Version:
      5.0
    • Resolved In Build:
      b110
    • CPU:
      generic
    • OS:
      generic
    • Verification:
      Verified

      Description

      A DESCRIPTION OF THE REQUEST :
      springframework 3.1.2 makes heavily use of
      java.lang.Class.getAnnotations() when instantiating Objects using loadTimeWeaving.
      On our multicore-machines (24 cores) the heavily use causes a bottleneck because getAnnotations() internally uses a synchronized method that checks if the cache is filled or stale.
      Looking at the sourcecode of java.lang.Class, without much internal knowledge, it looks like, that a kind of double-checked locking could avoid the entry of the synchronized method most of the time:
      - put cached annotations in local variable
      - check if variable is null or the caches need to be resettet because of class-redefinition, by looking at the two counters
      - if everything ist fine return local variable
      - else enter the synchronized block like now.

      Probably initAnnotationsIfNecessary needs to be restructured for use with local variables to avoid concurrency-Effects during the Construction of the cached annotations-List.

      Theoretically there could occur a class redefinition of the class between check and return of the value, but a concurrency-Problem like that could always occur while asking something about the class an before continuing another thread could alter the class.

      The current code looks like it could give wrong data for annotations, if one thread clears/refills the cache during initAnnotationsIfnecessary(), while the other just finished initAnnotationsIfnecessary() and goes on with
      return AnnotationParser.toArray(annotations)



      JUSTIFICATION :
      The enhancement is necessary because of bottleneck behaviour on multicore machines that often ask for annotations of the same class.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      No synchronization while calling getAnnotations as long as the class is not redefined.
      ACTUAL -
      Every Call to getAnnotations is synchronized causing bottleneck behaviour.

      ---------- BEGIN SOURCE ----------
      Executor-Pool with a lot of threads calling getAnnotations for some Class.
      ---------- END SOURCE ----------

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                webbuggrp Webbug Group
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: