什么是“虚假失败” on AtomicInteger weakCompareAndSet是什么意思? [英] What does "spurious failure" on AtomicInteger weakCompareAndSet mean?

查看:102
本文介绍了什么是“虚假失败” on AtomicInteger weakCompareAndSet是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java AtomicInteger类有一个方法 -

The Java AtomicInteger class has a method -

boolean weakCompareAndSet(int expect,int update)

其文件说:


5月虚假地失败。

May fail spuriously.

这里'虚假失败'是什么意思?

What does 'failing spuriously' here mean?

推荐答案

虚假地:无明显原因

根据 atomic package javadoc:

According to atomic package javadoc:


原子类还支持weakCompareAndSet方法,其适用性有限。

The atomic classes also support method weakCompareAndSet, which has limited applicability.

在某些平台上,弱版本在正常情况下可能比compareAndSet更有效,但不同之处在于任何给定的weakCompareAndSet方法调用都可能返回false虚假(即 ,无明显原因 )。

On some platforms, the weak version may be more efficient than compareAndSet in the normal case, but differs in that any given invocation of the weakCompareAndSet method may return false spuriously (that is, for no apparent reason).

错误返回仅表示如果需要可以重试操作,依赖于当变量保持expectedValue并且没有其他线程也试图设置时重复调用的保证变量最终会成功。

(例如,这种虚假失败可能是由于与预期值和当前值相等无关的内存争用效应。)

A false return means only that the operation may be retried if desired, relying on the guarantee that repeated invocation when the variable holds expectedValue and no other thread is also attempting to set the variable will eventually succeed.
(Such spurious failures may for example be due to memory contention effects that are unrelated to whether the expected and current values are equal.)

此外,weakCompareAndSet不提供同步控制通常需要的排序保证。

Additionally weakCompareAndSet does not provide ordering guarantees that are usually needed for synchronization control.






根据这个主题,它不是因为硬件/操作系统,而是因为weakCompareAndSet使用的基础算法:


According to this thread, it is not so much because of "hardware/OS", but because of the underlying algorithm used by weakCompareAndSet :


如果当前值==期望值,则weakCompareAndSet以原子方式将值设置为给定的更新值。可能是虚假的失败。

weakCompareAndSet atomically sets the value to the given updated value if the current value == the expected value. May fail spuriously.

与compareAndSet()和AtomicX上的其他操作不同, weakCompareAndSet()操作不会创建任何在排序前发生

Unlike compareAndSet(), and other operations on an AtomicX, the weakCompareAndSet() operation does not create any happens-before orderings.

因此,仅仅因为线程看到由weakCompareAndSet引起的对AtomicX的更新并不意味着它与weakCompareAndSet()之前发生的操作正确同步。

Thus, just because a thread sees an update to an AtomicX caused by a weakCompareAndSet doesn't mean it is properly synchronized with operations that occurred before the weakCompareAndSet().

您可能不想使用此方法,而应该只使用compareAndSet;因为很少有情况下weakCompareAndSet比compareAndSet快,并且在许多情况下,尝试使用weakCompareAndSet而不是compareAndSet来优化代码会导致细微且难以在代码中重现同步错误。

You probably don't want to use this method, but instead should just use compareAndSet; as there are few cases where weakCompareAndSet is faster than compareAndSet, and there are a number of cases in which trying to optimizing your code by using weakCompareAndSet rather than compareAndSet will introduce subtle and hard to reproduce synchronization errors into your code.






注意 在排序前发生


Note regarding happens-before orderings:


Java内存模型(JMM)定义了读取变量的线程保证在另一个线程中看到写入结果的条件。

The Java Memory Model (JMM) defines the conditions under which a thread reading a variable is guaranteed to see the results of a write in another thread.

JMM定义了一个名为happen-before的程序操作的顺序。

The JMM defines an ordering on the operations of a program called happens-before.

发生在线程之前的排序只能通过同步公共锁或访问公共volatile变量来创建。

Happens-before orderings across threads are only created by synchronizing on a common lock or accessing a common volatile variable.

在没有事先排序的情况下,Java平台有很大的自由度来延迟或改变一个线程中的写入对于读取相同内容的读取的顺序在另一个变量。

In the absence of a happens-before ordering, the Java platform has great latitude to delay or change the order in which writes in one thread become visible to reads of that same variable in another.

这篇关于什么是“虚假失败” on AtomicInteger weakCompareAndSet是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆