ConcurrentHashMap--未完成

xiaoxiao2021-02-28  174

2、源代码

public class ConcurrentHashMap<K,V> extends AbstractMap<K,V> implements ConcurrentMap<K,V>, Serializable { //容器最大长度 private static final int MAXIMUM_CAPACITY = 1 << 30; //默认长度 private static final int DEFAULT_CAPACITY = 16; //数组可能最大值,需要与toArray()相关方法关联 static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; //并发级别,遗留下来的,为兼容以前的版本 private static final int DEFAULT_CONCURRENCY_LEVEL = 16; //链表长度超过TREEIFY_THRESHOLD,转为红黑树 static final int TREEIFY_THRESHOLD = 8; //红黑树长度<UNTREEIFY_THRESHOLD,退化为链表 static final int MIN_TREEIFY_CAPACITY = 64; //help resize的最大线程数 private static final int MAX_RESIZERS = (1 << (32 - RESIZE_STAMP_BITS)) - 1; //元素类 static class Node<K,V> implements Map.Entry<K,V> { final int hash; final K key; //value volatile V val; volatile Node<K,V> next; Node(int hash, K key, V val, Node<K,V> next) { this.hash = hash; this.key = key; this.val = val; this.next = next; } public final K getKey() { return key; } public final V getValue() { return val; } public final int hashCode() { return key.hashCode() ^ val.hashCode(); } public final String toString(){ return key + "=" + val; } public final V setValue(V value) { throw new UnsupportedOperationException(); } public final boolean equals(Object o) { Object k, v, u; Map.Entry<?,?> e; return ((o instanceof Map.Entry) && (k = (e = (Map.Entry<?,?>)o).getKey()) != null && (v = e.getValue()) != null && (k == key || k.equals(key)) && (v == (u = val) || v.equals(u))); } /** * Virtualized support for map.get(); overridden in subclasses. */ Node<K,V> find(int h, Object k) { Node<K,V> e = this; if (k != null) { do { K ek; if (e.hash == h && ((ek = e.key) == k || (ek != null && k.equals(ek)))) return e; } while ((e = e.next) != null); } return null; } } //根据hash获取下标 与HashMap的类似 static final int spread(int h) { return (h ^ (h >>> 16)) & HASH_BITS; } //获取比c大的最小2的次方数 private static final int tableSizeFor(int c) { int n = c - 1; n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; n |= n >>> 8; n |= n >>> 16; return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1; } // Unsafe mechanics private static final sun.misc.Unsafe U; private static final long SIZECTL; private static final long TRANSFERINDEX; private static final long BASECOUNT; private static final long CELLSBUSY; private static final long CELLVALUE; private static final long ABASE; private static final int ASHIFT; static { try { //可以在运行时操作jvm数据 U = sun.misc.Unsafe.getUnsafe(); Class<?> k = ConcurrentHashMap.class; //sizeCtl字段在对象中的偏移 SIZECTL = U.objectFieldOffset (k.getDeclaredField("sizeCtl")); TRANSFERINDEX = U.objectFieldOffset (k.getDeclaredField("transferIndex")); BASECOUNT = U.objectFieldOffset (k.getDeclaredField("baseCount")); CELLSBUSY = U.objectFieldOffset (k.getDeclaredField("cellsBusy")); Class<?> ck = CounterCell.class; CELLVALUE = U.objectFieldOffset (ck.getDeclaredField("value")); Class<?> ak = Node[].class; //table数组在对象中的偏移地址 ABASE = U.arrayBaseOffset(ak); //获取数组中一个元素的大小(指针的大小,对象的大小都为4) int scale = U.arrayIndexScale(ak); //元素长度必须为2的次方对齐 if ((scale & (scale - 1)) != 0) throw new Error("data type scale not a power of two"); //最高位的1的偏移数、用代码打印一下就看得出来 ASHIFT = 31 - Integer.numberOfLeadingZeros(scale); } catch (Exception e) { throw new Error(e); } } }
转载请注明原文地址: https://www.6miu.com/read-17574.html

最新回复(0)