Concurrenthashmap the true meaning of synchronous security (Stringbuff is synchronous and secure, stringbutter unsafe)

Source: Internet
Author: User

Synchronous security Collection, it is safe to use this map in multithreading, but what does this security mean? Not to say that now 2 threads to use this map, the result of this map I do not have to add synchronize synchronization lock, not so, multi-threaded or to add this synchronization lock, because the map itself is not the security of thread security, when the map value is a Vo object, And this object in the multi-threaded changes, or need to synchronize lock, and map itself is the thread security is the ability to delete the element in the map traversal, modify can, but delete some map can be not, No, map deletion must be error: Java.util.ConcurrentModificationException, this is because HASHMAP, list and other collections are unsafe collections, traversal can not be deleted, and Concurrenthashmap is a multi-threaded security map collection that can be deleted when traversed. As to why can not be deleted because HashMap, list and so on is traversed through the itator, and its time is the element is irreversible, and the yoke, when it is a delete, such as the synchronization of external list or HashMap when not found The corresponding elements in the list or HashMap will be error-related. That unsafe collection when to use, one is single-threaded under can be used, but still can not be deleted all over the time, but also one is such as the base template encapsulation collection, this collection will only get not deleted, this situation can choose the non-synchronous security collection. So whether single-threaded or multi-threaded, it is almost always best to use a thread-safe collection (a collection that is only used for get is not deleted except that it can be used in a non-secure collection, such as a collection of game-based data).

Concurrenthashmap the true meaning of synchronous security (Stringbuff is synchronous and secure, stringbutter unsafe)

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.