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

AtomicReferenceFieldUpdater does not support static fields

XMLWordPrintable

      FULL PRODUCT VERSION :
      java version "1.8.0_144"
      Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
      Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      debian linux 9.1 on amd64. kernel is debian 4.9.30-2+deb9u3

      A DESCRIPTION OF THE PROBLEM :
      java.util.concurrent.atomic.AtomicReferenceFieldUpdater does not support static fields. When attempting to access a static field it attempts to get the offset with sun.misc.Unsafe.objectFieldOffset which then throws an IllegalArgumentException as it is only for non-static fields.

      Either the documentation should be updated to make it clear that static fields are not supported or, perferably, AtomicReferenceFieldUpdater should be updated to support static fields.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Call AtomicReferenceFieldUpdater.newUpdater(...) on a static field. A illegalArugmentException is thrown.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I was expecting to be able to utilize a AtomicReferenceFiledUpdater on a static field. The documentation neither says it does or does not. As static fields are supported with normal reflection I expected to be able to use the AtomicReferenceFieldUpdater on a static field.
      ACTUAL -
      An IllegalArgumentException was thrown.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      java.lang.IllegalArgumentException
              at sun.misc.Unsafe.objectFieldOffset(Native Method)
              at java.util.concurrent.atomic.AtomicReferenceFieldUpdater$AtomicReferenceFieldUpdaterImpl.<init>(AtomicReferenceFieldUpdater.java:363)
              at java.util.concurrent.atomic.AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdater.java:110)


      REPRODUCIBILITY :
      This bug can be reproduced always.

      CUSTOMER SUBMITTED WORKAROUND :
      There are many ways to work around this. It Depends on your design.

            dl Doug Lea
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated: