Dowemo
0 0 0 0

2, source code

publicclassConcurrentHashMap<K,V> extendsAbstractMap<K,V>
 implementsConcurrentMap<K,V>, Serializable {//容器最大长度privatestaticfinalint MAXIMUM_CAPACITY = 1 <<30;
 //默认长度privatestaticfinalint DEFAULT_CAPACITY = 16;
 //数组可能最大值,需要与toArray()相关方法关联staticfinalint MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
 //并发级别,遗留下来的,为兼容以前的版本privatestaticfinalint DEFAULT_CONCURRENCY_LEVEL = 16;
 //链表长度超过TREEIFY_THRESHOLD,转为红黑树staticfinalint TREEIFY_THRESHOLD = 8;
 //红黑树长度<UNTREEIFY_THRESHOLD,退化为链表staticfinalint MIN_TREEIFY_CAPACITY = 64;
 //help resize的最大线程数privatestaticfinalint MAX_RESIZERS = (1 <<(32 - RESIZE_STAMP_BITS)) - 1;
 //元素类staticclassNode<K,V> implementsMap.Entry<K,V> {finalint 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;
 }
 publicfinal K getKey() { return key; }
 publicfinal V getValue() { return val; }
 publicfinalint hashCode() { return key.hashCode() ^ val.hashCode(); }
 publicfinal String toString(){ return key + "=" + val; }
 publicfinal V setValue(V value) {
 thrownew UnsupportedOperationException();
 }
 publicfinalboolean 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);
 }
 returnnull;
 }
 }
 //根据hash获取下标 与HashMap的类似staticfinalint spread(int h) {
 return (h ^ (h>> > 16)) & HASH_BITS;
 }
 //获取比c大的最小2的次方数privatestaticfinalint 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 mechanicsprivatestaticfinal sun.misc.Unsafe U;
 privatestaticfinallong SIZECTL;
 privatestaticfinallong TRANSFERINDEX;
 privatestaticfinallong BASECOUNT;
 privatestaticfinallong CELLSBUSY;
 privatestaticfinallong CELLVALUE;
 privatestaticfinallong ABASE;
 privatestaticfinalint 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)
 thrownew Error("data type scale not a power of two");
 //最高位的1的偏移数、用代码打印一下就看得出来 ASHIFT = 31 - Integer.numberOfLeadingZeros(scale);
 } catch (Exception e) {
 thrownew Error(e);
 }
 }
}



Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs