- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
Java AtomicInteger 类有一个方法 -
boolean weakCompareAndSet(int expect,int update)
它的文档说:
May fail spuriously.
这里的“虚假失败”是什么意思?
最佳答案
虚假地:没有明显的原因
根据 atomic
包 javadoc:
The atomic classes also support method weakCompareAndSet, which has limited applicability.
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).
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.)Additionally weakCompareAndSet does not provide ordering guarantees that are usually needed for synchronization control.
根据 this thread ,与其说是因为“硬件/操作系统”,不如说是因为 weakCompareAndSet 使用的底层算法:
weakCompareAndSet atomically sets the value to the given updated value if the current value == the expected value. May fail spuriously.
Unlike compareAndSet(), and other operations on an AtomicX, the weakCompareAndSet() operation does not create any happens-before orderings.
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().
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.
关于的注意事项happens-before orderings :
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.
The JMM defines an ordering on the operations of a program called happens-before.
Happens-before orderings across threads are only created by synchronizing on a common lock or accessing a common volatile variable.
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.
关于java - AtomicInteger weakCompareAndSet 上的 "spurious failure"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/355365/
当返回值不感兴趣时,AtomicInteger.getAndIncrement() 之间是否有任何(甚至在实践中不相关)差异?和 AtomicInteger.incrementAndGet()方法
有没有AtomicInteger.accumulateAndGet()不能用AtomicInteger.updateAndGet()代替的场景,还是只是为了方便方法引用? 这是一个简单的示例,我没有看
我对java中的并发数据结构有疑问,特别是: 1) ConcurrentHashMap 2) HashMap 3) ConcurrentHashMap 如果我理解正确的话: 1) 读/写是线程安全的,
我怎样才能实现这里的 while 循环总是精确执行 100 次。当我执行代码时,在极少数情况下它会在控制台上打印 99 或 98 行而不总是 100,我不明白。 import java.util.Ar
我从原子学教程中获取这段代码,它说:- "By using AtomicInteger as a replacement for Integer we're able to increment the
我正在尝试为我的 Libgdx 游戏创建一个简单的十秒倒计时器。我创建了一个 timekeep 类,它实现了 runnable 并且可以很好地执行十秒倒计时。然而,我想打印这个 Timer 变量,因为
如果两个线程都使用 i++ 递增相同的 int i,我们可能会遇到问题,因为 i++ 不是原子操作。这就是为什么有 AtomicInteger.increment(),它使递增原子化。因此,如果我们有
我已经阅读了 java.util.concurrent 包的 API 文档,但显然误解了一些内容。概述说 A small toolkit of classes that support lock-fr
是否 AtomicInteger提供任何形式的公平保证?像先到先服务的线程执行顺序就可以了? Victor Grazi's concurrent animated 上的动画示例绝对没有表现出任何这样的
我有一个类,它在各自的线程上创建许多新对象,我想保持跨线程的运行计数。我想要一个 AtomicInteger 但它没有达到我的预期,而只是得到了一个较小的版本。我假设这是一个竞争条件错误 - 但我不完
为什么在AtomicInteger或另一个Atomic类中定义了这个set方法? 示例代码: /** * Sets to the given value. * * @param newValu
我怀疑下面是否是线程安全的, // is this thread safe, final int MAX_COUNT = 3 ? if (retryCount.get() < MAX_COUNT) {
我正在阅读一些关于 Java 中的原子变量的文档。正如到处都写的那样,AtomicInteger 应该是线程安全的。 根据我对原子整数的理解,它的工作原理是比较和交换算法。当两个线程试图在完全相同的时
import java.util.concurrent.atomic.AtomicInteger; public class Volatiletest extends Thread { pri
AtomicIntegers 被认为是同步原语,还是它只是 Java 提供的方法(wait()、notify() 等)。 我对基元的定义感到困惑,因为原子整数可以对 int 进行操作并提供无锁线程 s
public class SynchroExample { public final AtomicInteger integer = new AtomicInteger(0); priva
我有一个 android 客户端,它将与服务器建立 Http 连接。 服务器要求所有的Http请求在Http头中提供一个单调递增的计数器。例如 POST /foo/server X-count: 43
我读到过使用原子包类使我们能够进行线程安全、更少锁定的编码。但是我不太确定原子包类中的方法如何在不使用锁或任何同步关键字的情况下提供线程安全。任何帮助都将不胜感激。 最佳答案 他们使用非常低级的指令,
我想实现一个共享对象来计算操作执行的统计信息。对象状态将由 Map 表示(键是操作的名称,值是操作执行的次数)。我可以选择 HashMap 是否正确?由于 AtomicInteger 具有 volat
我想找出从 0 到 1000000 的所有质数。为此我写了这个愚蠢的方法: public static boolean isPrime(int n) { for(int i = 2; i <
我是一名优秀的程序员,十分优秀!