Concurrent HashMap source code analysis – java8

< H4 id = "1currenthashmap feature" > 1 Concurrenthashmap property

Note: because the word of concurrenthashmap is too long, the following applies CHM instead of concurrenthashmap

Hash value calculation spread (int h) hash value = (H ^ (H > > > 16)) & hash_ BITS;// Eliminate the influence of the highest bit of the XOR result. Analysis of the hashcode method of H: key: the hash value is unsigned and shifted to the right by 16 bits. Reason: because table uses a mask with an integer power of 2, the hash sets that change only in the bits above the current mask will always collide. (for example, the set of float keys keeps continuous integers in a small table) so we apply a transformation to extend the influence of high bits down. This is a balance between speed, performance and distribution. Because many common hash sets have been reasonably distributed (they will not benefit from the spread mechanism) because we have used the red black tree to deal with a large number of collisions in the bin, we just do some shifts in the simplest way, and then perform XOR operation to reduce system loss, And merge the influence of the highest bit that will not be used for index calculation due to table boundary (i.e. & operation). Capacity expansion method: tablesizefor (int c) C: C = 1.5 * capacity + 1; Return value: > = the integer power of the first 2 of C. method analysis: if C is the integer power of 2, return C; If C is not an integer power of 2, the first integer power of 2 greater than C is returned; Eg: C = 16, the returned result is 16; C = 30, the returned result is 32; Description of the three methods for accessing table: they all belong to volatile methods, so even in the process of resizing, the results obtained by accessing the elements in the table are correct. The call to settabat() method always occurs in the lock area, so in principle, complete volatile semantics are not required, but the current code conservatively selects the volatile method. Methods static final node tabat (node [] tab, int i) static final Boolean castatat (node [] tab, int i, node C, node V) static final void settabat (node [] tab, int i, node V) bin array: transient volatile node [] table; Sizectl: a variable used to control table initialization and resize. Negative value: table initializing or resizing sizecl = - 1: initializing; Sizecl = - (1 + n): Currently, n threads are resizing; When the table is uninitialized, the initial table size used during creation is saved, or the default is 0. After initialization, save the count value of the next element to resize the table. 4 constructors concurrenthashmap() concurrenthashmap (int initialCapacity) concurrenthashmap (map)

package sourcecode. analysis;

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>