Relaxed write, what does it mean?

216 Views Asked by At
private static class Node<E> {
    volatile E item;
    volatile Node<E> next;

    /**
     * Constructs a new node.  Uses relaxed write because item can
     * only be seen after publication via casNext.
     */
    Node(E item) {
        UNSAFE.putObject(this, itemOffset, item);
    }

It comes from java.util.concurrent.ConcurrentLinkedQueue.java

What does it mean, relaxed write?

1

There are 1 best solutions below

0
On BEST ANSWER

It is a good question. But the clue to understanding this term lies in ... JDK 9, where sun.misc.Unsafe has become (or will become - what is more appropriate to say, see here) public API.

In JDK 9 the corresponding place, which you refers to, is implemented as follows:

static final class Node<E> {
    volatile E item;
    volatile Node<E> next;

    /**
     * Constructs a node holding item.  Uses relaxed write because
     * item can only be seen after piggy-backing publication via CAS.
     */
    Node(E item) {
        ITEM.set(this, item);
    }
    ...
}

Where ITEM is instance of VarHandle class which implements similar functionality as sun.misc.Unsafe. Now, we can look at this method's JavaDoc description and find the following:

Sets the value of a variable to the newValue, with memory semantics of setting as if the variable was declared non-volatile and non-final. Commonly referred to as plain write access.

In other words, we can conclude, that relaxed write is the same as plain write access. In other words, I believe that Michael's commentary above is right:

... its like the opposite of volatile - a write that is not guaranteed to be seen across threads.

(see the opposite method setVolatile which works as if the variable was declared volatile).