本文介绍了Java中的ConcurrentHashMap和Hashtable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java中的ConcurrentHashMap和Hashtable有什么不同?



对于线程应用程序更高效?

解决方案

ConcurrentHashMap 使用多个桶来存储数据。这样可以避免读锁,并且通过 HashTable 大大提高了性能。两者都是线程安全的,但是在 ConcurrentHashMap 中有明显的性能优势。当您从 ConcurrentHashMap 使用 get(),没有锁,与 HashTable 所有操作都是简单同步的。
HashTable 是在旧版Java中发布的,而 ConcurrentHashMap 是java 5+的东西。



HashMap 是在单线程应用程序中使用的最佳选择。


What is the difference between a ConcurrentHashMap and a Hashtable in Java?

Which is more efficient for threaded applications?

解决方案

ConcurrentHashMap uses multiple buckets to store data. This avoids read locks and greatly improves performance over a HashTable. Both are thread safe, but there are obvious performance wins with ConcurrentHashMap.

When you read from a ConcurrentHashMap using get(), there are no locks, contrary to the HashTable for which all operations are simply synchronized.HashTable was released in old versions of Java whereas ConcurrentHashMap is a java 5+ thing.

HashMap is the best thing to use in a single threaded application.

这篇关于Java中的ConcurrentHashMap和Hashtable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 11:09