In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of how to write the implementation code of the java collection, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading the implementation code of this java collection. Let's take a look at it.
1. HashMappublic class HashMapDemo {private Map map = null; public void init () {map = new HashMap (); map.put ("a", "aaa"); map.put ("b", "bbb"); map.put ("c", "ccc"); System.out.println (map) } / / add elements public void testPut () {/ V put (K key, V value): add the specified key and value to the collection map.put ("A1", "aaa"); map.put ("b1", "bbb"); map.put ("C1", "ccc"); System.out.println (map) / / void putAll (Map e = tab [index]; e! = null; e = e.next) {if ((e.hash = = hash) & & e.key.equals (key)) {return (V) e.value;}} return null;}
The above is the get source code of Hashtable, we can see that it only adds locks on the method, which greatly reduces the execution efficiency of the thread, in the form of sacrificing efficiency to achieve the goal, which is obviously not what we want in practice, so we need a method that can not only be guaranteed in thread safety, but also in efficiency.
ConcurrentHashMap uses the principle of segmented locking, and we look at the source code.
Public V put (K key, V value) {return putVal (key, value, false);} final V putVal (K key, V value, boolean onlyIfAbsent) {if (key = = null | | value = = null) throw new NullPointerException (); int hash = spread (key.hashCode ()); int binCount = 0; for (Node [] tab = table;;) {Node f; int n, I, fh If (tab = = null | | (n = tab.length) = 0) tab = initTable (); else if ((f = tabAt (tab, I = (n-1) & hash)) = = null) {if (tab (tab, I, null, new Node (hash, key, value, null)) break / / no lock when adding to empty bin} else if ((fh = f.hash) = = MOVED) tab = helpTransfer (tab, f); else {V oldVal = null Synchronized (f) {if (tabAt (tab, I) = = f) {if (fh > = 0) {binCount = 1; for (Node e = f; + binCount) {K ek If (e.hash = = hash & & ((ek = e.key) = = key | | (ek! = null & & key.equals (ek) {oldVal = e.val If (! onlyIfAbsent) e.val = value; break;} Node pred = e If ((e = e.next) = = null) {pred.next = new Node (hash, key, value, null); break } else if (f instanceof TreeBin) {Node p; binCount = 2 If ((p = (TreeBin) f) .putTreeVal (hash, key, value))! = null) {oldVal = p.val If (! onlyIfAbsent) p.val = value }} if (binCount! = 0) {if (binCount > = TREEIFY_THRESHOLD) treeifyBin (tab, I) If (oldVal! = null) return oldVal; break;} addCount (1L, binCount); return null;}
You can see from the source code that ConcurrentHashMap only adds locks when there are threads to manipulate the current data, so the efficiency is greatly improved.
Efficiency is improved in the case of thread safety.
This is the end of the article on "how to write the implementation code of the java collection". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to write the implementation code of the java collection". If you want to learn more knowledge, you are welcome to follow the industry information channel.
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.