Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Is hashmap thread safe?

Shulou Source: shulou.com Published: 2022-06-01 06:43:14 10月05日 Update

This article mainly talks about "is hashmap thread safe?", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "is hashmap thread safe?"

Jdk1.8 is now widely used. In fact, to simulate the multithreading of jdk1.8 is not safe, it is actually very simple.

Copy a copy of the hashmap code. Then the capacity of hashmap is fixed to 1, and expansion is prohibited.

For example: modify the resize code. It can be expanded for the first time, and the capacity is 1. Not the second time.

Final Node [] resize () {if (FirstResize.get ()! = 0) {System.out.println ("> cannot be extended >"); return table;} else {FirstResize.addAndGet (1);}

In the put method, you can simulate the scheduling of control threads.

/ * * Implements Map.put and related methods * * @ param hash hash for key * @ param key the key * @ param value the value to put * @ param onlyIfAbsent if true, don't change existing value * @ param evict if false, the table is in creation mode. * @ return previous value, or null if none * / final V putVal (int hash, K key, V value, boolean onlyIfAbsent, boolean evict) {Node [] tab; Node p; int n, I; if ((tab = table) = = null | | (n = tab.length) = = 0) n = (tab = resize ()) .length If ((p = tab [I = (n-1) & hash]) = null) {/ / Multithreading concurrency, resulting in data overwriting if (Thread.currentThread (). GetName (). Equals ("a")) {System.out.println ("Thread A hibernates, pauses execution") / / simulate to override the values of other threads try {Thread.sleep (5x1000);} catch (InterruptedException e) {e.printStackTrace ();}} System.out.println ("I >" + I) System.out.println ("insert:" + key + ":" + value); tab [I] = newNode (hash, key, value, null);} else {Node e; K k If (p.hash = = hash & & (k = p.key) = = key | | (key! = null & & key.equals (k) e = p; else if (p instanceof TreeNode) e = ((TreeNode) p) .putTreeVal (this, tab, hash, key, value); else {for (int binCount = 0; + + binCount) {if ((e = p.next) = = null) {/ / similarly, the case of being overwritten when concurrent put occurs p.next = newNode (hash, key, value, null) If (binCount > = TREEIFY_THRESHOLD-1) / /-1 for 1st treeifyBin (tab, hash); break } if (e.hash = = hash & & (k = e.key) = = key | | (key! = null & & key.equals (k) break; p = e }} if (e! = null) {/ / existing mapping for key V oldValue = e.value; if (! onlyIfAbsent | | oldValue = = null) e.value = value; afterNodeAccess (e); return oldValue;}} + + modCount If (+ + size > threshold) resize (); afterNodeInsertion (evict); return null;}

Test the code:

Package com.interview.javabasic.myhashmap;import java.util.Map;import java.util.concurrent.atomic.AtomicInteger;public class TestThreadMap {private static HashMap mh=new HashMap (1); public static void main (String [] args) {HashMapThread thread0 = new HashMapThread ("a", mh); HashMapThread thread1 = new HashMapThread ("b", mh); thread0.start (); thread1.start () Try {Thread.sleep (1501000);} catch (InterruptedException e) {e.printStackTrace ();} / at this point you will find that there is only 2Jing 2 in map. / / mh.put (3 name 3); covered with System.out.println (mh);}} class HashMapThread extends Thread {HashMap mh; private String namet; public HashMapThread (String name, HashMap mh) {super (name); this.namet=name; this.mh=mh } @ Override public void run () {if (namet.equals ("b")) {try {Thread.sleep (2x 1000);} catch (InterruptedException e) {e.printStackTrace ();} mh.put (3jol 3);} else {mh.put (2m 2) At this point, I believe you have a deeper understanding of "is hashmap thread safe?". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Threads safety code content capacity methods learning practicality deeper unease interest only practicality reality situation simple operation data time more friends Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Shulou Tech Info MariaDB Shulou Information Apple