In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article Xiaobian introduces in detail "ConcurrentHashMap source code analysis in Java8", the content is detailed, the steps are clear, and the details are handled properly. I hope this "ConcurrentHashMap source code analysis in Java8" article can help you solve your doubts.
ConcurrentHashMap is a thread-safe version of HashMap that supports efficient concurrency.
Container initialization
1. Source code analysis
There are five constructors in jdk8's ConcurrentHashMap. None of these four constructors initializes the internal array, but only deals with the initial values of some variables.
The array initialization of jdk8's ConcurrentHashMap is done when the element is first added.
/ / there is no operation to maintain any variables. If this method is called, the array length defaults to 16public ConcurrentHashMap () {} / / passing in an initial capacity. Based on this value, ConcurrentHashMap will calculate a power of 2 greater than this value as the initial capacity public ConcurrentHashMap (int initialCapacity) {if (initialCapacity).
< 0) throw new IllegalArgumentException(); int cap = ((initialCapacity >= (MAXIMUM_CAPACITY > 1)? MAXIMUM_CAPACITY: tableSizeFor (initialCapacity + (initialCapacity > 1) + 1); / / the initial capacity here is calculated as half of the incoming capacity + 1 this.sizeCtl = cap;}
Note that when you call this method, the initial capacity is different from the ConcurrentHashMap of HashMap and jdk7. Even if you pass a power of 2, the initial capacity calculated by this method is still the power of 2 that is greater than this value.
/ / call the construction of the four parameters public ConcurrentHashMap (int initialCapacity, float loadFactor) {this (initialCapacity, loadFactor, 1);} / / calculate a value greater than or equal to the given capacity, which is the power of 2 as the initial capacity public ConcurrentHashMap (int initialCapacity, float loadFactor, int concurrencyLevel) {if (! (loadFactor > 0.0f) | | initialCapacity < 0 | | concurrencyLevel = (long) MAXIMUM_CAPACITY)? MAXIMUM_CAPACITY: tableSizeFor ((int) size); this.sizeCtl = cap;} / / based on a Map collection, build a ConcurrentHashMap// with an initial capacity of 16public ConcurrentHashMap (Map
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.