Difference between CocurrentHashMap and Hashtable, cocurrenthashmap

Source: Internet
Author: User

Difference between CocurrentHashMap and Hashtable, cocurrenthashmap

Collection classes are the core of Java APIs, but I think it is an art to make good use of them. I have summarized some personal experiences, such as using ArrayList to improve performance, rather than needing outdated vectors. JDK 1.5 introduces some easy-to-use concurrent collection classes, which are very useful for large e-commerce systems that require low latency. This article will look at the differences between ConcurrentHashMap and Hashtable.

This article describes how HashMap works and the differences between HashMap and Hashtable. If you have read this article, I believe you will gain some benefits after reading this article.

Why do we need ConcurrentHashMap and CopyOnWriteArrayList?

Synchronous collection class (Hashtable and Vector), synchronous encapsulation class (using Collections. synchronizedMap () method and Collections. the objects returned by the synchronizedList () method can be used to create a thread-safe Map and List. But some factors make them unsuitable for High-concurrency systems. They only have a single lock, lock the entire set, and often lock the set for a period of time during iteration to prevent ConcurrentModificationException. These features are all obstacles to scalability.

ConcurrentHashMap and CopyOnWriteArrayList keep thread security while providing higher concurrency. ConcurrentHashMap and CopyOnWriteArrayList are not used everywhere. In most cases, you only need HashMap and ArrayList to deal with common situations.

Difference between ConcurrentHashMap and Hashtable

What are the differences between Hashtable and ConcurrentHashMap? They can all be used in multi-threaded environments, but when the size of Hashtable increases to a certain extent, the performance will drop sharply, because the iteration needs to be locked for a long time. Because ConcurrentHashMap introduces segmentation, no matter how large it becomes, it only needs to lock a part of the map, and other threads do not need to wait until the iteration is complete to access map. In short, in the iteration process, ConcurrentHashMap only locks a part of the map, while Hashtable locks the entire map.

 

Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.