- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在实现一个请求实例的 FIFO 队列(为速度而预先分配的请求对象),并开始使用 add 方法上的“同步”关键字。该方法很短(检查固定大小缓冲区中是否有空间,然后将值添加到数组)。使用visualVM,线程似乎比我喜欢的更频繁地阻塞(准确地说是“监视器”)。因此,我将代码转换为使用 AtomicInteger 值来跟踪当前大小,然后在 while 循环中使用 compareAndSet()(就像 AtomicInteger 在内部对 incrementAndGet() 等方法所做的那样)。代码现在看起来更长了。
我想知道的是,使用同步且较短的代码与不带 synchronized 关键字的较长代码相比,性能开销是多少(因此永远不应阻塞锁定)。
这是带有 synchronized 关键字的旧 get 方法:
public synchronized Request get()
{
if (head == tail)
{
return null;
}
Request r = requests[head];
head = (head + 1) % requests.length;
return r;
}
这里是没有 synchronized 关键字的新 get 方法:
public Request get()
{
while (true)
{
int current = size.get();
if (current <= 0)
{
return null;
}
if (size.compareAndSet(current, current - 1))
{
break;
}
}
while (true)
{
int current = head.get();
int nextHead = (current + 1) % requests.length;
if (head.compareAndSet(current, nextHead))
{
return requests[current];
}
}
}
我的猜测是 synchronized 关键字更糟,因为有阻塞锁的风险(可能导致线程上下文切换等),即使代码更短。
谢谢!
最佳答案
My guess was the synchronized keyword is worse because of the risk of blocking on the lock (potentially causing thread context switches etc)
是的,一般情况下你是对的。 Java Concurrency in Practice在第 15.3.2 节中对此进行了讨论:
[...] at high contention levels locking tends to outperform atomic variables, but at more realistic contention levels atomic variables outperform locks. This is because a lock reacts to contention by suspending threads, reducing CPU usage and synchronization traffic on the shared memory bus. (This is similar to how blocking producers in a producer-consumer design reduces the load on consumers and thereby lets them catch up.) On the other hand, with atomic variables, contention management is pushed back to the calling class. Like most CAS-based algorithms,
AtomicPseudoRandom
reacts to contention by trying again immediately, which is usually the right approach but in a high-contention environment just creates more contention.Before we condemn
AtomicPseudoRandom
as poorly written or atomic variables as a poor choice compared to locks, we should realize that the level of contention in Figure 15.1 is unrealistically high: no real program does nothing but contend for a lock or atomic variable. In practice, atomics tend to scale better than locks because atomics deal more effectively with typical contention levels.The performance reversal between locks and atomics at differing levels of contention illustrates the strengths and weaknesses of each. With low to moderate contention, atomics offer better scalability; with high contention, locks offer better contention avoidance. (CAS-based algorithms also outperform lock-based ones on single-CPU systems, since a CAS always succeeds on a single-CPU system except in the unlikely case that a thread is preempted in the middle of the read-modify-write operation.)
(在文中提到的数字上,图 15.1 显示了 AtomicInteger 和 ReentrantLock 在竞争高时的性能或多或少相等,而图 15.2 显示在中等竞争下前者的性能比后者高出 1 倍2-3.)
正如其他人所指出的,非阻塞算法虽然可能更快,但更复杂,因此更难正确处理。 JCiA 第 15.4 节的提示:
Good nonblocking algorithms are known for many common data structures, including stacks, queues, priority queues, and hash tables, though designing new ones is a task best left to experts.
Nonblocking algorithms are considerably more complicated than their lock-based equivalents. The key to creating nonblocking algorithms is figuring out how to limit the scope of atomic changes to a single variable while maintaining data consistency. In linked collection classes such as queues, you can sometimes get away with expressing state transformations as changes to individual links and using an
AtomicReference
to represent each link that must be updated atomically.
关于java - 在 Java 中,AtomicInteger compareAndSet() 与 synchronized 关键字的性能如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3556283/
Java 通过其原子类公开 CAS 操作,例如 boolean compareAndSet(expected,update) JavaDocs指定 compareAndSet 操作的内存效果如下: c
假设您有以下类(class) public class AccessStatistics { private final int noPages, noErrors; public Acces
正在学习并发编程类(class)。 举个例子 final class Counter { private AtomicInteger value; public lon
我想知道原子类中 set() 和 compareAndSet() 的区别。 set() 方法是否也保证了原子过程?例如这段代码: public class sampleAtomic{ priv
今天我在采访中被问到下一个问题:“如果您在处理器不支持 CAS 操作的机器上调用 AtomicLong 的 compareAndSet 方法,会发生什么情况”。 能否请您帮我解决这个问题,并在可能的情
我正在实现一个简单的缓存,并将缓存存储为 AtomicReference。 private AtomicReference> cacheData; 应该从数据库表中(延迟地)填充缓存对象。 我提供了一
我正在试验 java.util.concurrent 并试图找出如何正确使用 AtomicReference.compareAndSet 来管理对单个共享状态单元的并发访问。 特别是:compareA
spring-data-redis 模块包含 RedisAtomicLong 类。 在这个类中你可以看到 public boolean compareAndSet(long expect, long
From the Java AtomicReferenceFieldUpdater docs : Note that the guarantees of the compareAndSet metho
源码是一样的。 public final boolean compareAndSet(V expect, V update) { return unsafe.compareAndSwapObj
在阅读 Java 中 java.util.Random 类的文档时,我偶然发现了 next 方法中的一些东西,我无法完全理解。 protected int next(int bits) { l
Java 的 AtomicInteger 提供 public final boolean compareAndSet(int expect, int update) .如果返回false,我想知道比较
线程标题应该是不言自明的......我对 AtomicBoolean 类的以下方法的规范有点困惑: java.util.concurrent.atomic.AtomicBoolean#compareA
我希望将 AtomicLong 中的当前 timestamp 与 currentTimeMS 进行比较,以便我知道是否已经过去了一段时间,如果是,则只有一个单线程将进入一个代码块,但据我所知,comp
来自AtomicLong的源代码: public final boolean compareAndSet(long expect, long update) { return
因为 Atomic 意味着线程安全。当 .set() 本身在 Java 中是原子和线程安全的时,我们什么时候使用 compareAndSet? 举例来说,我想自动设置一个变量,这样每个其他线程都可以看
static boolean unsynchronizedSetter(Date expected){ Date newDate = new Date(); AtomicReferen
VarHandle 显示以下错误 - Exception in thread "main" java.lang.NoSuchMethodError: VarHandle.compareAndSet(V
我正在实现一个请求实例的 FIFO 队列(为速度而预先分配的请求对象),并开始使用 add 方法上的“同步”关键字。该方法很短(检查固定大小缓冲区中是否有空间,然后将值添加到数组)。使用visualV
我想知道调用之间是否有任何区别(或可能的副作用): AtomicBoolean.set(true) 和 AtomicBoolean.compareAndset(false, true) AtomicB
我是一名优秀的程序员,十分优秀!