Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use ConcurrentHashMap

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to use ConcurrentHashMap, I hope you will get something after reading this article. Let's discuss it together.

First take a look at the putVal method

If (tab = = null | | (n = tab.length) = = 0) tab = initTable ()

If you don't already have table, initialize table first.

Private final Node [] initTable () {Node [] tab; int sc; while ((tab = table) = = null | | tab.length = = 0) {if ((sc = sizeCtl))

< 0) Thread.yield(); // lost initialization race; just spin else if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) { try { if ((tab = table) == null || tab.length == 0) { int n = (sc >

0)? Sc: DEFAULT_CAPACITY; @ SuppressWarnings ("unchecked") Node [] nt = (Node []) new Node [n]; table = tab = nt; / / size is controlled at 0.75 sc = n-(n > 2) }} finally {sizeCtl = sc;} break;}} return tab;}

This code is relatively simple. SizeCtl is a very important attribute in the whole process, which will be encountered many times in the process of capacity expansion, initialization and so on. It also acts as an exclusive lock here, and when it is-1, other threads wait.

Else if ((f = tabAt (tab, I = (n-1) & hash)) = = null) {if (casTabAt (tab, I, null, new Node (hash, key, value, null)) break; / / no lock when adding to empty bin}

If the slot to be inserted is empty, then insert it directly.

Else if ((fh = f.hash) = = MOVED) tab = helpTransfer (tab, f)

So if the hash value to be inserted is the moved state of-1, then the helpTransfer method should be executed, yes, to help expand the capacity first. We are going to pull out a lot of things here, and we will analyze them bit by bit.

First of all, let's see when the hash value of a node becomes-1. All the way down, there are only

Static final class ForwardingNode extends Node

This class is used, and it has a property in it

Final Node [] nextTable

You can probably see from here that ConcurrentHashmap is in a state of expansion at this time, and the expanded table can be found through ForwardingNode.

Let's take a look at helpTransfer.

Final Node [] helpTransfer (Node [] tab, Node f) {Node [] nextTab; int sc; / / again check whether the current node is ForwardingNode, because if not, there is no way to find nextTable, so there is no way to help expand if (tab! = null & & (f instanceof ForwardingNode) & & (nextTab = ((ForwardingNode) f) .nextTable)! = null) {int rs = resizeStamp (tab.length) While (nextTab = = nextTable & & table = = tab & & (sc = sizeCtl)

< 0) { if ((sc >

> RESIZE_STAMP_SHIFT)! = rs | | sc = = rs + 1 | | sc = = rs + MAX_RESIZERS | | transferIndex 1)? (n > 3) / NCPU: n)

< MIN_TRANSFER_STRIDE) stride = MIN_TRANSFER_STRIDE; // subdivide range if (nextTab == null) { // initiating try { @SuppressWarnings("unchecked") Node[] nt = (Node[])new Node[n = bound || finishing) advance = false; // 扩容时用的指针已经小于0,则结束 else if ((nextIndex = transferIndex) stride ? nextIndex - stride : 0))) { bound = nextBound; i = nextIndex - 1; advance = false; } } // i 小于 0 ,已经结束了 if (i < 0 || i >

= n | | I + n > = nextn) {int sc; / / if it is over, set table to nextTable if (finishing) {nextTable = null; table = nextTab; sizeCtl = (n > > 1); return } / / indicates that the current thread has finished its work SizeCtl-1 if (U.compareAndSwapInt (this, SIZECTL, sc = sizeCtl, sc-1)) {if ((sc-2)! = resizeStamp (n) = 0) {/ / Why fh & n does the & operation here because the binary of 15 is 1111 16 is 10000 31 is 11111 / / There is only the first bit before and after capacity expansion & it will change after operation, and the other bits will remain the same, so it is fine with table.length & int runBit = fh & n. Node lastRun = f; / / traverse first to determine the ni-> n rehash equal segment, so that the next time the slot is reallocated, this section will no longer traverse the for (Node p = f.next; p! = null; p = p.next) {int b = p.hash & n If (b! = runBit) {runBit = b; lastRun = p;}} if (runBit = = 0) {ln = lastRun Hn = null;} else {hn = lastRun; ln = null;} for (Node p = f; p! = lastRun P = p.next) {int ph = p.hash; K Competition = p.key; V pv = p.vale; if ((ph & n) = = 0) ln = new Node (ph, Competition, pv, ln) Else hn = new Node (ph, competition, pv, hn);} setTabAt (nextTab, I, ln); setTabAt (nextTab, I + n, hn) / / Mark completed nodes as fwd setTabAt (tab, I, fwd); advance = true;} else if (f instanceof TreeBin) {TreeBin t = (TreeBin) f TreeNode lo = null, loTail = null; TreeNode hi = null, hiTail = null; int lc = 0, hc = 0; for (Node e = t.first; e! = null; e = e.next) {int h = e.hash TreeNode p = new TreeNode (h, e.key, e.val, null, null); if ((h & n) = = 0) {if ((p.prev = loTail) = = null) lo = p Else loTail.next = p; loTail = p; + + lc } else {if ((p.prev = hiTail) = = null) hi = p; else hiTail.next = p HiTail = p; + + hc;}} ln = (lc = NCPU) collide = false / / At max size or stale else if (! collide) collide = true / / acquire the lock And expand the capacity of CounterCell else if (cellsBusy = = 0 & & U.compareAndSwapInt (this, CELLSBUSY, 0) 1)) {try {if (counterCells = = as) {/ / Expand table unless stale CounterCell [] rs = new CounterCell [n = (long) (sc = sizeCtl) & & (tab = table)! = null & & (n = tab.length) < MAXIMUM_CAPACITY)

Here it is judged that s is sumCount, that is, baseCount plus the sum of each node as the total. If s is greater than sizeCtl or table is not empty and does not reach the maximum value, the capacity expansion operation is carried out.

After reading this article, I believe you have a certain understanding of "how to use ConcurrentHashMap". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report