In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article, "what are the ways to achieve thread safety in Map in Java?" so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this article, "what are the ways for Map to achieve thread safety in Java?"
Method 1. Use HashtableMap hashtable=new Hashtable ()
This is the first thing that comes to mind, so why is it thread-safe? Let's take a look at its source code. We can see that our commonly used methods such as put,get,containsKey are synchronous, so it is thread-safe.
Public synchronized boolean containsKey (Object key) {Entry tab [] = table; int hash = key.hashCode (); int index = (hash & 0x7FFFFFFF)% tab.length; for (Entry e = tab [index]; e! = null; e = e.next) {if ((e.hash = = hash) & & e.key.equals (key)) {return true }} return false;} public synchronized V get (Object key) {Entry tab [] = table; int hash = key.hashCode (); int index = (hash & 0x7FFFFFFF)% tab.length; for (Entry e = tab [index]; e! = null E = e.next) {if ((e.hash = = hash) & & e.key.equals (key)) {return (V) e.value;}} return null;} public synchronized V put (K key, V value) {/ / Make sure the value is not null if (value = = null) {throw new NullPointerException () } / / Makes sure the key is not already in the hashtable. Entry tab [] = table; int hash = key.hashCode (); int index = (hash & 0x7FFFFFFF)% tab.length; @ SuppressWarnings ("unchecked") Entry entry = (Entry) tab [index]; for (; entry! = null; entry = entry.next) {if ((entry.hash = = hash) & & entry.key.equals (key)) {V old = entry.value Entry.value = value; return old;}} addEntry (hash, key, value, index); return null;}
The implementation principle is that the synchronized lock mechanism is used in the method of adding, deleting, changing and querying. In the multithreaded environment, whether reading data or modifying data, only one thread can execute the synchronize method at the same time, because the whole table is locked. So the more threads there are, the fiercer the competition for the map is, the less efficient it is, and it is not recommended.
Method 2. Use Collections.synchronizedMap (new Hashtable ())
Its implementation principle is to use the static method in the tool class to package the incoming Hashtable into synchronization, that is, the synchronized mechanism is added to the method of adding, deleting, modifying and searching, and its implementation is similar to that of Hashtable, and its efficiency is similar, so it is not recommended.
Map map = Collections.synchronizedMap (new Hashtable ())
The following is the JDK source code
Public static Map synchronizedMap (Map m) {return new SynchronizedMap (m);} private static class SynchronizedMap implements Map, Serializable {private static final long serialVersionUID = 1978198479659022715L; private final Map m; / / Backing Map final Object mutex; / / Object on which to synchronize SynchronizedMap (Map m) {this.m = Objects.requireNonNull (m); mutex = this } SynchronizedMap (Map m, Object mutex) {this.m = m; this.mutex = mutex;} public int size () {synchronized (mutex) {return m.size ();}} public boolean isEmpty () {synchronized (mutex) {return m.isEmpty () } public boolean containsKey (Object key) {synchronized (mutex) {return m.containsKey (key);}} public boolean containsValue (Object value) {synchronized (mutex) {return m.containsValue (value);}} public V get (Object key) {synchronized (mutex) {return m.get (key) } public V put (K key, V value) {synchronized (mutex) {return m.put (key, value);}} public V remove (Object key) {synchronized (mutex) {return m.remove (key);}} public void putAll (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.