JCTools簡介

JCTools是一款對jdk併發數據結構進行加強的併發工具,主要提供了map以及queue的加強數據結構。原來netty仍是本身寫的MpscLinkedQueueNode,後來新版本就換成使用JCTools的併發隊列了。html

加強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設計的。只有幾個方法java

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主要是操做以後沒有當即返回git

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-vectorgithub

原來是數據結構

// --- 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;
  }

doc

相關文章
相關標籤/搜索