轻松写,什么意思?
Relaxed write, what does it mean?
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);
}
来自java.util.concurrent.ConcurrentLinkedQueue.java
轻松写是什么意思?
这是个好问题。但是理解这个词的线索在于... JDK 9,其中 sun.misc.Unsafe
已经成为(或将成为 - 更合适的说法,参见 here) public API.
在JDK9中你引用的对应的地方实现如下:
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);
}
...
}
其中 ITEM
是 VarHandle
class 的实例,它实现了与 sun.misc.Unsafe
类似的功能。现在,我们可以查看此方法的 JavaDoc description 并找到以下内容:
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.
换句话说,我们可以得出结论,轻松写入与普通写入访问相同。换句话说,我相信Michael上面的评论是正确的:
... its like the opposite of volatile - a write that is not guaranteed
to be seen across threads.
(参见相反的方法 setVolatile
,它的工作方式就好像变量被声明为 volatile
)。
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); }
来自java.util.concurrent.ConcurrentLinkedQueue.java
轻松写是什么意思?
这是个好问题。但是理解这个词的线索在于... JDK 9,其中 sun.misc.Unsafe
已经成为(或将成为 - 更合适的说法,参见 here) public API.
在JDK9中你引用的对应的地方实现如下:
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);
}
...
}
其中 ITEM
是 VarHandle
class 的实例,它实现了与 sun.misc.Unsafe
类似的功能。现在,我们可以查看此方法的 JavaDoc description 并找到以下内容:
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.
换句话说,我们可以得出结论,轻松写入与普通写入访问相同。换句话说,我相信Michael上面的评论是正确的:
... its like the opposite of volatile - a write that is not guaranteed to be seen across threads.
(参见相反的方法 setVolatile
,它的工作方式就好像变量被声明为 volatile
)。