在以下代码中涉及到java的一个unsafe类,正如它的名字一样,它是java的一个不安全类,因为它有很多很多本地方法都是涉及到内存操作的,不正确的使用它会造成一些难以发现的错误。
private final boolean compareAndSetHead(Node update) {
// CAS方法中的前两个参数是确定待更新数据的内存位置,第三个是预期值,第四个是将被更新为的值
// 如果从内存中读出的值和预期的值不同,那么cas失败,否则成功。
return unsafe.compareAndSwapObject(this, headOffset, null, update);
}
/**
* CAS tail field. Used only by enq.
*/
private final boolean compareAndSetTail(Node expect, Node update) {
return unsafe.compareAndSwapObject(this, tailOffset, expect, update);
}
/**
* CAS waitStatus field of a node.
*/
private static final boolean compareAndSetWaitStatus(Node node,
int expect,
int update) {
return unsafe.compareAndSwapInt(node, waitStatusOffset,
expect, update);
}
/**
* CAS next field of a node.
*/
private static final boolean compareAndSetNext(Node node,
Node expect,
Node update) {
return unsafe.compareAndSwapObject(node, nextOffset, expect, update);
}
AQS源码解析
最新推荐文章于 2022-05-13 12:23:56 发布