Re: Are there real use cases with the Java access modes?
Benjamin Manes via Concurrency-interest <[email protected]> Sat, 17 Jul 2021 15:18:22 -0700
| Newsgroups | gmane.comp.java.jsr.166-concurrency |
|---|---|
| Message-ID | <CAGu0=MNaeL=PRP7McRoR+wVHi68NZ2Z1-y10eYA7FPFYneMCdg@mail.gmail.com> |
There is one usage of acquire / release in Caffeine for a weak / soft
values cache. It looks something like the below snippet. It has the
following characteristics:
1. During construction, a plain set is used as the entry is not yet visible
to other threads. That can piggyback on the lock release.
2. Reading the value can usually use a plain read to piggyback on the map's
volatile read of the entry, of which this class is the entry's value object.
3. When the referent is null, it may either be due to the garbage collector
or a cache.put(k,v) setting a new reference and nulling out the old one. An
acquire is used to check for a stale read.
4. When writing, a release is used to ensure that clearing the reference is
ordered after the value is set. This way an intermediate null is not read
due to a compiler reordering.
In general, though, most developers would prefer to use stronger orderings
than necessary as simpler to reason about with equivalent performance.
There probably are not many valid use-cases outside of the JDK, and all
others (including below) should probably be considered suspect.
----
static final VarHandle VALUE;
Node(Object keyReference, V value, ReferenceQueue<V> referenceQueue) {
VALUE.set(this, new WeakValueReference<V>(keyReference, value,
referenceQueue));
}
public final V getValue() {
for (;;) {
Reference<V> ref = (Reference<V>) VALUE.get(this);
V referent = ref.get();
if ((referent != null) || (ref == VALUE.getAcquire(this))) {
return referent;
}
}
}
public final void setValue(V value, ReferenceQueue<V> referenceQueue) {
Reference<V> ref = (Reference<V>) VALUE.get(this);
VALUE.setRelease(this, new WeakValueReference<V>(getKeyReference(),
value, referenceQueue));
ref.clear();
}
On Sat, Jul 17, 2021 at 1:32 PM Shuyang Liu via Concurrency-interest <
[email protected]> wrote:
> Hello,
>
> My colleagues and I have been working on a formal model for the Java
> access modes that is added since Java 9 [1]. Are there any popular use
> cases of access modes in real world applications/frameworks/libraries? We
> would like to see how our current formal model works in real examples.
>
> Thanks you,
> Shuyang Liu
>
> [1]. Using JDK 9 Memory Order Modes
> http://gee.cs.oswego.edu/dl/html/j9mm.html
> _______________________________________________
> Concurrency-interest mailing list
> [email protected]
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>
_______________________________________________
Concurrency-interest mailing list
[email protected]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest