读书人

concurrent包原子变量种中的compareAn

发布时间: 2012-11-03 10:57:44 作者: rapoo

concurrent包原子变量类中的compareAndSet与weakCompareAndSet方法

public final boolean compareAndSet(int expect, int update) {return unsafe.compareAndSwapInt(this, valueOffset, expect, update);}/** * May [color=red]fail spuriously[/color] and [color=red]does not provide ordering guarantees[/color] */public final boolean weakCompareAndSet(int expect, int update) {return unsafe.compareAndSwapInt(this, valueOffset, expect, update);}


原子变量类中的compareAndSet与weakCompareAndSet方法从源代码来看实现一模一样。
但是jdk中对weakCompareAndSet的注释 fail spuriously, does not provide ordering guarantees让人有些迷糊。

为什么weakCompareAndSet会在compareAndSet有效的情况下发生fail spuriously,是由于does not provide ordering guarantees造成的吗。这里的does not provide ordering guarantees是指内存指令重排序造成的吗?

两个方法相同的实现,都是对unsafe进行相同的调用,为什么会有差别呢?

迷糊,蛋疼中...

----------------------------

看来这是一个“坑爹”的注释,jdk1.6中并未实现weakCompareAndSet功能,而其注释所指fail spuriously, does not provide ordering guarantees应该是内存指令重排序造成的。

读书人网 >编程

热点推荐