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

Add Stable Field Updaters to allow efficient lazy field evaluations

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P3 P3
    • tbd
    • 25
    • core-libs
    • None

      It would be beneficial if there was a way to create StableFieldUpdater constructs that can be used to safely manipulate stable instance fields. Only one updater is needed per class, similar to `AtomicIntegerFieldUpdater`.

      Here is an example of how such a construct could be used:


          public final class LazyFoo {

               private final Bar bar;
               private final Baz baz;

               private static final ToIntFunction<LazyFoo> HASH_UPDATER =
                       StableFieldUpdater.ofInt(LazyFoo.class, "hash",
                               l -> Objects.hash(l.bar, l.baz), -1);

               @Stable
               private int hash;

               public LazyFoo(Bar bar, Baz baz) {
                   this.bar = bar;
                   this.baz = baz;
               }

               @Override
               public boolean equals(Object o) {
                   return o instanceof Foo that &&
                           Objects.equals(this.bar, that.bar) &&
                           Objects.equals(this.baz, that.baz);
               }

               @Override
               public int hashCode() {
                   return HASH_UPDATER.applyAsInt(this);
               }
           }

            pminborg Per-Ake Minborg
            pminborg Per-Ake Minborg
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: