首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

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

2012-11-03 
concurrent包原子变量类中的compareAndSet与weakCompareAndSet方法public final boolean compareAndSet(in

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应该是内存指令重排序造成的。

热点排行