- A+
所属分类:其他教程
文章目录[隐藏]
- ConcurrentHashMap
- 同步的HashMap
- ConcurrentHashMap和Synchronized HashMap的区别。
ConcurrentHashMap
和SynchronizedHashMap
都是线程安全的集合类,可以在多线程和并发的java应用程序中使用。但是它们之间存在着一些区别。在这篇文章中,我们来看看它们之间的所有这些差异。
ConcurrentHashMap
ConcurrentHashMap
是一个实现了ConcurrentMap
接口的类。它使用Hashtable,下划线数据结构。我们知道,在我们的应用程序中处理线程时,由于性能问题,HashMap不是一个好的选择。为了解决这个问题,我们在应用程序中使用ConcurrentHashMap。ConcurrentHashMap
是线程安全的,因此多个线程可以毫无问题地对一个对象进行操作。在ConcurrentHashMap
中,对象根据并发级别被划分为若干段。默认情况下,它允许16个线程在没有任何同步的情况下从Map中读和写。在ConcurrentHashMap中,任何数量的线程都可以在同一时间进行检索操作,但是对于对象的更新,线程必须锁定该线程想要操作的特定段。这种类型的锁定机制被称为段锁定或桶锁定。因此,在同一时间,线程可以进行16次更新操作。
ConcurrentHashMap示例代码:
import java.util.*; import java.util.concurrent.*; public class TraversingConcurrentHashMap { public static void main(String[] args) { // create an instance of ConcurrentHashMap ConcurrentHashMap<Integer, String> chmap = new ConcurrentHashMap<Integer, String>(); // Add elements using put() chmap.put(110, "Yiibai"); chmap.put(210, "for"); chmap.put(310, "Geeks"); chmap.put(410, "Welcome"); chmap.put(510, "vsdiffer"); // Create an Iterator over the // ConcurrentHashMap Iterator<ConcurrentHashMap.Entry<Integer, String> > itr = chmap.entrySet().iterator(); while (itr.hasNext()) { ConcurrentHashMap.Entry<Integer, String> entry = itr.next(); System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); } } }
运行结果如下:
Key = 510, Value = vsdiffer Key = 210, Value = for Key = 410, Value = Welcome Key = 110, Value = Yiibai Key = 310, Value = Geeks
同步的HashMap
Java HashMap是一个非同步的集合类。如果我们需要对它进行线程安全的操作,那么必须明确地对它进行同步。java.util.Collections
类的synchronizedMap()
方法被用来同步它。它返回一个以指定映射为后盾的同步(线程安全)映射。
同步HashMap示例代码:
import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class SynchronizedHashMap { public static void main(String args[]) { // Creating a HashMap HashMap<Integer, String> hmap = new HashMap<Integer, String>(); // Adding the elements using put method hmap.put(110, "yiibai"); hmap.put(210, "for"); hmap.put(310, "Geeks"); hmap.put(215, "Welcome"); hmap.put(410, "vsdiffer.com"); // Creating a synchronized map Map map = Collections.synchronizedMap(hmap); Set set = map.entrySet(); // Synchronize on HashMap, not on set synchronized (map) { Iterator i = set.iterator(); // Printing the elements while (i.hasNext()) { Map.Entry me = (Map.Entry)i.next(); System.out.print(me.getKey() + ": "); System.out.println(me.getValue()); } } } }
运行结果如下:
210: for 410: vsdiffer.com 215: Welcome 110: yiibai 310: Geeks
ConcurrentHashMap和Synchronized HashMap的区别。
ConcurrentHashMap | Synchronized HashMap |
---|---|
ConcurrentHashMap 是一个实现了ConcurrentMap 和可序列化接口的类。 |
可以通过使用java.util.Collections类的synchronizedMap()方法来同步HashMap。 |
它锁定了映射的某些部分。 | 它锁定了整个映射。 |
ConcurrentHashMap 允许执行并发的读和写操作。因此,性能相对来说比同步映射要好。 |
在Synchronized HashMap中,多个线程不能并发访问映射。因此,性能相对低于ConcurrentHashMap。 |
ConcuurentHashMap 不允许插入空作为键或值。 |
同步HashMap允许插入null作为键。 |
ConccurentHashMap 不抛出ConcurrentModificationException。 |
同步HashMap抛出ConcurentHashMap 的ConcurrentModificationException。 |
- 我的微信公众号
- 扫一扫关注
-
- 我的新浪微博号
- 扫一扫关注
-