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:2initialCapacity:3, capacity:4initialCapacity:4, capacity:4initialCapacity:5, capacity:8initialCapacity:6, capacity:8initialCapacity:7, capacity:8initialCapacity:8, capacity:8initialCapacity:9, capacity:16initialCapacity:10, capacity:16initialCapacity:11, capacity:16initialCapacity:13, capacity:16initialCapacity:15, capacity:16initialCapacity:16, capacity:16initialCapacity:17, capacity:32initialCapacity:26, capacity:32initialCapacity:31, capacity:32initialCapacity:32, capacity:32initialCapacity:33, capacity:64initialCapacity:50, capacity:64initialCapacity:60, capacity:64initialCapacity:63, capacity:64initialCapacity:64, capacity:64initialCapacity:65, capacity:128
ConcurrentHashMap.java
int concurrencyLevel = 16; //默认是16int 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]/*Segments0 = new Segment (loadFactor, (int)(cap * loadFactor), (HashEntry [])new HashEntry[cap]);Segment [] ss = (Segment [])new Segment[ssize];*/
输出结果:
initialCapacity:1, c:1, cap:2, ssize:16
initialCapacity:10, c:1, cap:2, ssize:16initialCapacity:16, c:1, cap:2, ssize:16initialCapacity:31, c:2, cap:2, ssize:16initialCapacity:32, c:2, cap:2, ssize:16initialCapacity:33, c:3, cap:4, ssize:16initialCapacity:63, c:4, cap:4, ssize:16initialCapacity:64, c:4, cap:4, ssize:16initialCapacity:65, c:5, cap:8, ssize:16initialCapacity:127, c:8, cap:8, ssize:16initialCapacity:128, c:8, cap:8, ssize:16initialCapacity:129, c:9, cap:16, ssize:16initialCapacity:256, c:16, cap:16, ssize:16initialCapacity:257, c:17, cap:32, ssize:16initialCapacity:512, c:32, cap:32, ssize:16initialCapacity:513, c:33, cap:64, ssize:16initialCapacity:1024, c:64, cap:64, ssize:16initialCapacity:1025, c:65, cap:128, ssize:16initialCapacity:2048, c:128, cap:128, ssize:16initialCapacity:4096, c:256, cap:256, ssize:16initialCapacity:8192, c:512, cap:512, ssize:16initialCapacity:16384, c:1024, cap:1024, ssize:16
2013-05-14
名品推荐: