HashMap.java
int capacity = 1; int initialCapacitys[] = {1,2,3,4,5,6,7,8,9,10,11,13,15,16,17,26,31,32,33,50,60,63,64,65}; for(int initialCapacity : initialCapacitys) { while (capacity < initialCapacity) capacity <<= 1; System.out.println("initialCapacity:" + initialCapacity + ", capacity:" + capacity); } /* this.loadFactor = loadFactor; threshold = (int)Math.min(capacity * loadFactor, MAXIMUM_CAPACITY + 1); table = new Entry[capacity]; */
输出结果:
initialCapacity:1, capacity:1
initialCapacity:2, capacity:2
initialCapacity:3, capacity:4
initialCapacity:4, capacity:4
initialCapacity:5, capacity:8
initialCapacity:6, capacity:8
initialCapacity:7, capacity:8
initialCapacity:8, capacity:8
initialCapacity:9, capacity:16
initialCapacity:10, capacity:16
initialCapacity:11, capacity:16
initialCapacity:13, capacity:16
initialCapacity:15, capacity:16
initialCapacity:16, capacity:16
initialCapacity:17, capacity:32
initialCapacity:26, capacity:32
initialCapacity:31, capacity:32
initialCapacity:32, capacity:32
initialCapacity:33, capacity:64
initialCapacity:50, capacity:64
initialCapacity:60, capacity:64
initialCapacity:63, capacity:64
initialCapacity:64, capacity:64
initialCapacity:65, capacity:128
ConcurrentHashMap.java
int concurrencyLevel = 16; //默认是16 int MIN_SEGMENT_TABLE_CAPACITY = 2; int sshift = 0; int ssize = 1; while (ssize < concurrencyLevel) { ++sshift; ssize <<= 1; } int initialCapacitys[] = {1,10,16,31,32,33,63,64,65,127,128,129,256,257,512,513,1024,1025,2048,4096,8192,16384}; for(int initialCapacity : initialCapacitys) { int c = initialCapacity / ssize; if (c * ssize < initialCapacity) ++c; int cap = MIN_SEGMENT_TABLE_CAPACITY; while (cap < c) cap <<= 1; System.out.println("initialCapacity:" + initialCapacity + ", c:" + c + ", cap:" + cap + ", ssize:" + ssize); } // create segments and segments[0] /* Segment<K,V> s0 = new Segment<K,V>(loadFactor, (int)(cap * loadFactor), (HashEntry<K,V>[])new HashEntry[cap]); Segment<K,V>[] ss = (Segment<K,V>[])new Segment[ssize]; */
输出结果:
initialCapacity:1, c:1, cap:2, ssize:16
initialCapacity:10, c:1, cap:2, ssize:16
initialCapacity:16, c:1, cap:2, ssize:16
initialCapacity:31, c:2, cap:2, ssize:16
initialCapacity:32, c:2, cap:2, ssize:16
initialCapacity:33, c:3, cap:4, ssize:16
initialCapacity:63, c:4, cap:4, ssize:16
initialCapacity:64, c:4, cap:4, ssize:16
initialCapacity:65, c:5, cap:8, ssize:16
initialCapacity:127, c:8, cap:8, ssize:16
initialCapacity:128, c:8, cap:8, ssize:16
initialCapacity:129, c:9, cap:16, ssize:16
initialCapacity:256, c:16, cap:16, ssize:16
initialCapacity:257, c:17, cap:32, ssize:16
initialCapacity:512, c:32, cap:32, ssize:16
initialCapacity:513, c:33, cap:64, ssize:16
initialCapacity:1024, c:64, cap:64, ssize:16
initialCapacity:1025, c:65, cap:128, ssize:16
initialCapacity:2048, c:128, cap:128, ssize:16
initialCapacity:4096, c:256, cap:256, ssize:16
initialCapacity:8192, c:512, cap:512, ssize:16
initialCapacity:16384, c:1024, cap:1024, ssize:16
2013-05-14