Introduction to JCTools



JCTools is a concurrent tool to enhance the jdk concurrent data structure. It mainly provides the enhanced data structure of map and queue. Netty was originally a MpscLinkedQueueNode written by herself, but later the new version was replaced by a concurrent queue using JCTools.

Enhanced map

  • ConcurrentAutoTable(The foundation of the following map/set structures)
  • NonBlockingHashMap
  • NonBlockingHashMapLong
  • NonBlockingHashSet
  • NonBlockingIdentityHashMap
  • NonBlockingSetInt

Enhanced queue

  • 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)




Instead of AtomicLong, it is specially designed for high-performance counter. There are only a few ways

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();

Compared with AtomicLong, it is mainly that it does not return immediately after the operation.

public final long incrementAndGet();
public final long decrementAndGet()


NonBlockingHashMap is an enhancement to ConcurrentHashMap, providing better performance for multi-CPU support and high concurrent updates.
NonBlockingHashMapLong is NonBlockingHashMap with Long key.
NonBlockingHashSet is a simple wrapper for NonBlockingHashMap to support set interface.
NonBlockingIdentityHashMap is modified from NonBlockingHashMap and uses System.identityHashCode () to calculate hashes
NonBlockingSetInt is a simple bit-vector that uses CAS.

It turned out to be

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