Java 无锁并发算法在高性能场景中的三大核心应用
大家好,今天我们来聊聊Java无锁并发算法在高性能场景中的应用。在多核处理器普及的今天,并发编程变得越来越重要。传统的基于锁的并发控制虽然简单易懂,但在高并发场景下,锁竞争会带来性能瓶颈,例如上下文切换、锁的获取和释放等开销。无锁并发算法,也称为Lock-Free算法,则通过原子操作等机制,避免了锁的使用,从而提高了并发性能。
我们将围绕以下三个核心应用领域展开讨论:
- 高并发队列: 在生产者-消费者模型中,队列是常用的数据结构。使用无锁队列可以显著提升数据交换的效率。
- 并发计数器: 计数器在很多场景下都有应用,例如统计访问量、记录任务完成数量等。无锁计数器可以避免锁竞争带来的性能损失。
- 原子操作优化缓存: 利用原子操作更新缓存数据,可以减少锁的使用,提升缓存的并发访问能力。
一、高并发队列
在并发编程中,队列是重要的工具,用于线程间的数据传递。传统的阻塞队列依赖于锁来实现线程同步,在高并发场景下会产生较大的性能开销。无锁队列则利用原子操作避免了锁竞争,从而提高了并发性能。
1.1 基于CAS的无锁队列
CAS(Compare-and-Swap)是一种原子操作,它比较内存中的值与预期值,如果相等,则更新为新值。整个过程是原子的,不会被中断。基于CAS的无锁队列是常见的实现方式。
下面是一个基于CAS的无锁队列的简化实现:
import java.util.concurrent.atomic.AtomicReference;
public class ConcurrentQueue<T> {
private final AtomicReference<Node<T>> head;
private final AtomicReference<Node<T>> tail;
private static class Node<T> {
private final T value;
private final AtomicReference<Node<T>> next;
public Node(T value) {
this.value = value;
this.next = new AtomicReference<>(null);
}
}
public ConcurrentQueue() {
Node<T> dummyNode = new Node<>(null);
head = new AtomicReference<>(dummyNode);
tail = new AtomicReference<>(dummyNode);
}
public void enqueue(T value) {
Node<T> newNode = new Node<>(value);
while (true) {
Node<T> currentTail = tail.get();
Node<T> tailNext = currentTail.next.get();
if (currentTail == tail.get()) { // Check if tail is still the same
if (tailNext == null) {
if (currentTail.next.compareAndSet(null, newNode)) {
tail.compareAndSet(currentTail, newNode); // Try to advance tail
return;
}
} else {
// Another thread has inserted a new node after tail
tail.compareAndSet(currentTail, tailNext); // Try to advance tail
}
}
}
}
public T dequeue() {
while (true) {
Node<T> currentHead = head.get();
Node<T> currentTail = tail.get();
Node<T> headNext = currentHead.next.get();
if (currentHead == head.get()) { // Check if head is still the same
if (currentHead == currentTail) {
if (headNext == null) {
return null; // Queue is empty
}
// Another thread is enqueuing
tail.compareAndSet(currentTail, headNext); // Try to advance tail
} else {
T value = headNext.value;
if (head.compareAndSet(currentHead, headNext)) {
return value;
}
}
}
}
}
public static void main(String[] args) throws InterruptedException {
ConcurrentQueue<Integer> queue = new ConcurrentQueue<>();
Thread producer = new Thread(() -> {
for (int i = 0; i < 100; i++) {
queue.enqueue(i);
System.out.println("Enqueued: " + i);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
Thread consumer = new Thread(() -> {
for (int i = 0; i < 100; i++) {
Integer value = queue.dequeue();
if (value != null) {
System.out.println("Dequeued: " + value);
} else {
System.out.println("Queue is empty.");
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
i--; // Retry if queue is empty
}
}
});
producer.start();
consumer.start();
producer.join();
consumer.join();
System.out.println("Done.");
}
}
代码解释:
AtomicReference<Node<T>> head和AtomicReference<Node<T>> tail:使用原子引用来维护队列的头尾节点。enqueue(T value):入队操作。循环尝试将新节点添加到队列尾部。使用了双重检查来保证并发安全。如果当前尾节点的next为null,则尝试使用 CAS 将新节点设置为next。如果 CAS 成功,则尝试更新tail指针。dequeue():出队操作。循环尝试从队列头部移除节点。类似enqueue,也使用了双重检查和 CAS 操作。
CAS的ABA问题:
CAS操作依赖于比较内存中的值与预期值。如果一个变量的值从A变为B,然后再变回A,CAS操作会认为该变量没有被修改过。这就是ABA问题。
解决ABA问题:
- 版本号: 维护一个版本号,每次修改都增加版本号。CAS操作同时比较值和版本号。
- AtomicStampedReference: Java提供了
AtomicStampedReference类,可以用来解决ABA问题。它同时维护一个值和一个版本号。
1.2 Disruptor
Disruptor 是 LMAX 开发的一个高性能的异步消息处理框架。它基于环形缓冲区(Ring Buffer)实现,并采用了无锁并发技术。
Disruptor 的核心组件:
- Ring Buffer: 一个预先分配大小的环形缓冲区,用于存储消息。
- Sequence: 一个递增的序号,用于标识消息在 Ring Buffer 中的位置。
- Producer: 生产者,将消息写入 Ring Buffer。
- Consumer: 消费者,从 Ring Buffer 中读取消息并进行处理。
- EventProcessor: 消费者和 Ring Buffer 之间的桥梁,负责协调消息的读取和处理。
- EventHandler: 实际的消息处理逻辑。
Disruptor 的优势:
- 预分配内存: 避免了动态内存分配的开销。
- 环形缓冲区: 提高了缓存命中率。
- 无锁并发: 通过 CAS 操作和序号控制,避免了锁竞争。
- 批量处理: 支持批量读取和处理消息,提高了吞吐量。
Disruptor 的简单示例:
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.util.DaemonThreadFactory;
import java.nio.ByteBuffer;
public class DisruptorExample {
private static class LongEvent {
private long value;
public void set(long value) {
this.value = value;
}
public long getValue() {
return value;
}
}
private static class LongEventFactory implements com.lmax.disruptor.EventFactory<LongEvent> {
@Override
public LongEvent newInstance() {
return new LongEvent();
}
}
private static class LongEventHandler implements com.lmax.disruptor.EventHandler<LongEvent> {
@Override
public void onEvent(LongEvent event, long sequence, boolean endOfBatch) {
System.out.println("Event: " + event.getValue());
}
}
private static class LongEventProducer {
private final RingBuffer<LongEvent> ringBuffer;
public LongEventProducer(RingBuffer<LongEvent> ringBuffer) {
this.ringBuffer = ringBuffer;
}
public void onData(ByteBuffer bb) {
long sequence = ringBuffer.next(); // Grab the next sequence
try {
LongEvent event = ringBuffer.get(sequence); // Get the entry in the RingBuffer
event.set(bb.getLong(0)); // Fill with data
} finally {
ringBuffer.publish(sequence); // Make the data available to consumers
}
}
}
public static void main(String[] args) throws Exception {
// Specify the size of the ring buffer, must be power of 2.
int bufferSize = 1024;
// Construct the Disruptor
Disruptor<LongEvent> disruptor = new Disruptor<>(new LongEventFactory(), bufferSize, DaemonThreadFactory.INSTANCE);
// Connect the handler
disruptor.handleEventsWith(new LongEventHandler());
// Start the Disruptor, starts all threads running
disruptor.start();
// Get the ring buffer from the Disruptor to be used for publishing.
RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();
LongEventProducer producer = new LongEventProducer(ringBuffer);
ByteBuffer bb = ByteBuffer.allocate(8);
for (long l = 0; l < 10; l++) {
bb.putLong(0, l);
producer.onData(bb);
Thread.sleep(100);
}
disruptor.shutdown();
}
}
代码解释:
LongEvent:定义了消息的类型,这里是一个long值。LongEventFactory:用于创建LongEvent实例。LongEventHandler:实际的消息处理逻辑,这里只是简单地打印消息。LongEventProducer:生产者,负责将消息写入 Ring Buffer。main函数:- 创建 Disruptor 实例,指定 Ring Buffer 的大小、EventFactory 和线程池。
- 连接 EventHandler。
- 启动 Disruptor。
- 获取 Ring Buffer。
- 创建 Producer。
- 循环写入消息。
- 关闭 Disruptor。
二、并发计数器
计数器是常用的工具,用于统计各种数据。传统的基于锁的计数器在高并发场景下会产生较大的性能开销。无锁计数器则利用原子操作避免了锁竞争,从而提高了并发性能.
2.1 AtomicInteger
Java 提供了 java.util.concurrent.atomic.AtomicInteger 类,它是一个原子整型变量,提供了原子性的 get、set、incrementAndGet、decrementAndGet 等方法。
import java.util.concurrent.atomic.AtomicInteger;
public class AtomicCounter {
private final AtomicInteger counter = new AtomicInteger(0);
public int increment() {
return counter.incrementAndGet();
}
public int get() {
return counter.get();
}
public static void main(String[] args) throws InterruptedException {
AtomicCounter counter = new AtomicCounter();
Thread[] threads = new Thread[10];
for (int i = 0; i < threads.length; i++) {
threads[i] = new Thread(() -> {
for (int j = 0; j < 1000; j++) {
counter.increment();
}
});
threads[i].start();
}
for (Thread thread : threads) {
thread.join();
}
System.out.println("Counter: " + counter.get());
}
}
代码解释:
AtomicInteger counter:使用AtomicInteger来维护计数器的值。increment():原子性的递增计数器的值。get():获取计数器的值。main函数:- 创建多个线程,每个线程递增计数器 1000 次。
- 等待所有线程执行完毕。
- 打印计数器的值。
2.2 LongAdder (JDK 8+)
在高并发场景下,AtomicInteger 仍然可能存在竞争。JDK 8 引入了 java.util.concurrent.atomic.LongAdder 类,它通过将计数器拆分成多个单元,减少了竞争。
import java.util.concurrent.atomic.LongAdder;
public class LongAdderCounter {
private final LongAdder adder = new LongAdder();
public void increment() {
adder.increment();
}
public long get() {
return adder.sum();
}
public static void main(String[] args) throws InterruptedException {
LongAdderCounter counter = new LongAdderCounter();
Thread[] threads = new Thread[10];
for (int i = 0; i < threads.length; i++) {
threads[i] = new Thread(() -> {
for (int j = 0; j < 1000; j++) {
counter.increment();
}
});
threads[i].start();
}
for (Thread thread : threads) {
thread.join();
}
System.out.println("Counter: " + counter.get());
}
}
代码解释:
LongAdder adder:使用LongAdder来维护计数器的值。increment():递增计数器的值。get():获取计数器的值。LongAdder的sum()方法会将所有单元的值加起来。
LongAdder 的原理:
LongAdder 内部维护了一个或多个 Cell 对象,每个 Cell 对象都包含一个 long 类型的变量。当多个线程并发地递增计数器时,它们会尽量选择不同的 Cell 对象进行递增,从而减少竞争。当需要获取计数器的总值时,LongAdder 会将所有 Cell 对象的值加起来。
2.3 性能对比
| 操作 | AtomicInteger | LongAdder |
|---|---|---|
| 高并发写入 | 竞争激烈 | 竞争较小 |
| 读取总值 | 直接返回 | 需要累加 |
| 低并发写入 | 性能良好 | 稍逊于AtomicInteger |
在高并发写入场景下,LongAdder 的性能通常优于 AtomicInteger。但在低并发场景下,AtomicInteger 的性能可能更好,因为它避免了累加操作。
三、原子操作优化缓存
缓存是提高系统性能的重要手段。传统的基于锁的缓存在高并发场景下会产生较大的性能开销。利用原子操作更新缓存数据,可以减少锁的使用,提升缓存的并发访问能力。
3.1 ConcurrentHashMap + AtomicReference
ConcurrentHashMap 是一个线程安全的哈希表,提供了高效的并发访问能力。我们可以结合 ConcurrentHashMap 和 AtomicReference 来实现一个无锁缓存。
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;
public class AtomicCache<K, V> {
private final ConcurrentHashMap<K, AtomicReference<V>> cache = new ConcurrentHashMap<>();
public V get(K key) {
AtomicReference<V> valueRef = cache.get(key);
if (valueRef == null) {
return null;
}
return valueRef.get();
}
public V put(K key, V value) {
AtomicReference<V> newValueRef = new AtomicReference<>(value);
AtomicReference<V> oldValueRef = cache.putIfAbsent(key, newValueRef);
if (oldValueRef == null) {
// Key was absent, new value was added
return null;
} else {
// Key was present, return the old value
return oldValueRef.get();
}
}
public V computeIfAbsent(K key, java.util.function.Function<? super K, ? extends V> mappingFunction) {
while (true) {
AtomicReference<V> valueRef = cache.get(key);
if (valueRef != null) {
return valueRef.get();
}
V newValue = mappingFunction.apply(key);
AtomicReference<V> newValueRef = new AtomicReference<>(newValue);
AtomicReference<V> existingValueRef = cache.putIfAbsent(key, newValueRef);
if (existingValueRef == null) {
// Added successfully
return newValue;
} else {
// Another thread already added a value
// Retry
}
}
}
public static void main(String[] args) {
AtomicCache<String, Integer> cache = new AtomicCache<>();
// Put a value
cache.put("key1", 10);
// Get a value
Integer value1 = cache.get("key1");
System.out.println("Value for key1: " + value1);
// Compute if absent
Integer value2 = cache.computeIfAbsent("key2", k -> k.length() * 2);
System.out.println("Value for key2: " + value2);
Integer value3 = cache.get("key2");
System.out.println("Value for key2: " + value3);
}
}
代码解释:
ConcurrentHashMap<K, AtomicReference<V>> cache:使用ConcurrentHashMap来存储缓存数据,Value使用AtomicReference保证原子性。get(K key):从缓存中获取值。put(K key, V value):向缓存中添加值。使用putIfAbsent方法来保证只有一个线程能成功添加值。computeIfAbsent(K key, java.util.function.Function<? super K, ? extends V> mappingFunction):如果缓存中不存在指定键的值,则使用指定的计算函数计算值并添加到缓存中。使用了循环和putIfAbsent方法来保证并发安全。
缓存失效策略:
上述示例没有实现缓存失效策略。在实际应用中,需要考虑缓存失效策略,例如:
- 基于时间: 设置缓存的过期时间。
- 基于大小: 当缓存达到最大容量时,移除最近最少使用的数据。
- 主动失效: 当数据发生变化时,主动移除缓存。
3.2 Guava Cache
Guava Cache 是 Google Guava 库提供的一个高性能的缓存实现。它支持多种缓存失效策略,例如基于大小、基于时间等。
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
public class GuavaCacheExample {
public static void main(String[] args) throws ExecutionException {
LoadingCache<String, Integer> cache = CacheBuilder.newBuilder()
.maximumSize(100) // Maximum number of entries in the cache
.expireAfterWrite(10, TimeUnit.MINUTES) // Entries expire after 10 minutes of write
.build(
new CacheLoader<String, Integer>() {
@Override
public Integer load(String key) {
// Simulate loading data from a database or other source
System.out.println("Loading value for key: " + key);
return key.length() * 2;
}
});
// Get value from cache
Integer value1 = cache.get("key1");
System.out.println("Value for key1: " + value1);
// Get value from cache again
Integer value2 = cache.get("key1");
System.out.println("Value for key1: " + value2); // Will be fetched from cache
// Get value for a new key
Integer value3 = cache.get("key2");
System.out.println("Value for key2: " + value3);
try {
Thread.sleep(600000); // Wait for 10 minutes
} catch (InterruptedException e) {
e.printStackTrace();
}
// Attempt to get value after expiration
Integer value4 = cache.get("key1");
System.out.println("Value for key1 after expiration: " + value4); // Will be reloaded
}
}
代码解释:
CacheBuilder:用于构建缓存。maximumSize(100):设置缓存的最大容量为 100。expireAfterWrite(10, TimeUnit.MINUTES):设置缓存的过期时间为 10 分钟。CacheLoader:用于加载缓存数据。load(String key):加载指定键的值。get(String key):从缓存中获取值。如果缓存中不存在指定键的值,则会调用load方法加载数据。
四、总结一下无锁并发算法
无锁并发算法在高性能场景下具有重要的应用价值。通过避免锁的使用,可以减少锁竞争带来的性能开销,提高并发性能。在高并发队列、并发计数器和原子操作优化缓存等场景中,无锁并发算法都能够发挥重要的作用。选择合适的无锁并发算法需要根据具体的应用场景和性能需求进行权衡。