JCTools
早在96年就有論文提出了無鎖隊列的概念,再到後來 Disruptor,高性能已得到生產的驗證。此處介紹的 Jctools 中的高性能隊列,其性能絲毫不輸於 Disruptor。
JCTools (Java Concurrency Tools) 提供了一系列非阻塞併發數據結構(標準 Java 中缺失的),當存在線程爭搶的時候,非阻塞併發數據結構比阻塞併發數據結構能提供更好的性能。
JCTools 是一個開源工具包,在 Apache License 2.0 下發布,並在 Netty、Rxjava 等諸多框架中被廣泛使用。
JCTools 的開源 Github 倉庫:https://github.com/JCTools/JCTools
在 Maven 中引入 JCtools jar 包就能使用 JCTools 了:
<dependency>
<groupId>org.jctools</groupId>
<artifactId>jctools-core</artifactId>
<version>3.0.0</version>
</dependency>
JCTools 中主要提供了 Map 以及 Queue 的非阻塞併發數據結構:
非阻塞 Map
- ConcurrentAutoTable(後面幾個map/set結構的基礎)
- NonBlockingHashMap
- NonBlockingHashMapLong
- NonBlockingHashSet
- NonBlockingIdentityHashMap
- NonBlockingSetInt
NonBlockingHashMap 是對 ConcurrentHashMap 的增強,對多 CPU 的支持以及高併發更新提供更好的性能。
NonBlockingHashMapLong 是 key 爲 Long 型的 NonBlockingHashMap。
NonBlockingHashSet 是對 NonBlockingHashMap 的簡單包裝以支持 set 的接口。
NonBlockingIdentityHashMap 是從 NonBlockingHashMap 改造來的,使用 System.identityHashCode() 來計算哈希。
NonBlockingSetInt 是一個使用 CAS 的簡單的 bit-vector。
非阻塞 Queue
JCTools 提供的非阻塞隊列分爲 4 類,可以根據不同的應用場景選擇使用:
- SPSC-單一生產者單一消費者(有界和無界)
- MPSC-多生產者單一消費者(有界和無界)
- SPMC-單生產者多消費者(有界)
- MPMC-多生產者多消費者(有界)
“生產者”和“消費者”是指“生產線程”和“消費線程”。
// spsc-有界/無界隊列
Queue<String> spscArrayQueue = new SpscArrayQueue(16);
Queue<String> spscUnboundedArrayQueue = new SpscUnboundedArrayQueue(2);
// spmc-有界隊列
Queue<String> spmcArrayQueue = new SpmcArrayQueue<>(16);
// mpsc-有界/無界隊列
Queue<String> mpscArrayQueue = new MpscArrayQueue<>(16);
Queue<String> mpscChunkedArrayQueue = new MpscChunkedArrayQueue<>(1024, 8 * 1024);
Queue<String> mpscUnboundedArrayQueue = new MpscUnboundedArrayQueue<>(2);
// mpmc-有界隊列
Queue<String> mpmcArrayQueue = new MpmcArrayQueue<>(16);
JCTools是一款對jdk併發數據結構進行增強的併發工具,主要提供了map以及queue的增強數據結構。原來netty還是自己寫的MpscLinkedQueueNode,後來新版本就換成使用JCTools的併發隊列了。
增強map
- ConcurrentAutoTable(
後面幾個map/set結構的基礎
) - NonBlockingHashMap
- NonBlockingHashMapLong
- NonBlockingHashSet
- NonBlockingIdentityHashMap
- NonBlockingSetInt
增強隊列
- SPSC - Single Producer Single Consumer (Wait Free, bounded and unbounded)
- MPSC - Multi Producer Single Consumer (Lock less, bounded and unbounded)
- SPMC - Single Producer Multi Consumer (Lock less, bounded)
- MPMC - Multi Producer Multi Consumer (Lock less, bounded)
maven
<dependency>
<groupId>org.jctools</groupId>
<artifactId>jctools-core</artifactId>
<version>2.1.0</version>
</dependency>
ConcurrentAutoTable
替代AtomicLong,專門爲高性能的counter設計的。只有幾個方法
public void add( long x );
public void decrement();
public void increment();
public void set( long x );
public long get();
public int intValue();
public long longValue();
public long estimate_get();
對比AtomicLong主要是操作之後沒有立即返回
public final long incrementAndGet();
public final long decrementAndGet()
NonBlockingHashMap
NonBlockingHashMap是對ConcurrentHashMap的增強,對多CPU的支持以及高併發更新提供更好的性能。
NonBlockingHashMapLong是key爲Long型的NonBlockingHashMap。
NonBlockingHashSet是對NonBlockingHashMap的簡單包裝以支持set的接口。
NonBlockingIdentityHashMap是從NonBlockingHashMap改造來的,使用System.identityHashCode()來計算哈希
NonBlockingSetInt是一個使用CAS的簡單的bit-vector
原來是
// --- hash ----------------------------------------------------------------
// Helper function to spread lousy hashCodes. Throws NPE for null Key, on
// purpose - as the first place to conveniently toss the required NPE for a
// null Key.
private static final int hash(final Object key) {
int h = key.hashCode(); // The real hashCode call
h ^= (h>>>20) ^ (h>>>12);
h ^= (h>>> 7) ^ (h>>> 4);
h += h<<7; // smear low bits up high, for hashcodes that only differ by 1
return h;
}
改爲
// --- hash ----------------------------------------------------------------
// Helper function to spread lousy hashCodes
private static final int hash(final Object key) {
int h = System.identityHashCode(key); // The real hashCode call
// I assume that System.identityHashCode is well implemented with a good
// spreader, and a second bit-spreader is redundant.
//h ^= (h>>>20) ^ (h>>>12);
//h ^= (h>>> 7) ^ (h>>> 4);
return h;
}