Java Polygon Question-concurrency framework

Source: Internet
Author: User
Tags concurrentmodificationexception

1. What is the difference between Synchronizedmap and concurrenthashmap?

A: The JAVA5 has a new Concurrentmap interface and one of its implementation class Concurrenthashmap. The CONCURRENTHASHMAP provides a different locking mechanism than the Hashtable and Synchronizedmap. It provides much better concurrency than Synchronizedmap. Multiple reads can almost always be performed concurrently, while read and write operations can often be performed concurrently, while simultaneous write operations can still be performed concurrently (the related classes also provide concurrency of a similar number of read threads, but only one active write thread is allowed). The locking mechanism used in Hashtable is to lock the entire hash table at once, so that only one thread can manipulate it at the same time, while the Concurrenthashmap locks one bucket at a time. Concurrenthashmap By default, the hash table is divided into 16 buckets, such as get,put,remove, such as the common operation of only the current need to lock the bucket. This way, the original can only one thread into, but now can have 16 write thread execution, concurrency performance improvement is obvious. The 16 threads mentioned above refer to the write thread, while the read operation does not require a lock for most of the time.       It is only necessary to lock the entire hash table when working with size. In terms of iterations, Concurrenthashmap uses a different iterative approach. In this iterative approach, when iterator is created, the collection is no longer thrown concurrentmodificationexception and replaced by new data at the time of the change without affecting the original data. The iterator is completed before replacing the head pointer with the new data, so that the iterator thread can use the old data, and the write thread can complete the change concurrently.

2. What application scenarios can copyonwritearraylist be used for?
A: One of the benefits of copyonwritearraylist (lock-free containers) is that concurrentmodificationexception are not thrown when multiple iterators traverse and modify the list at the same time. In Copyonwritearraylist, writing causes a copy of the entire underlying array to be created, and the source array remains in place, allowing the read operation to be performed safely when the copied array is modified.

Java Polygon Question-concurrency framework

Related 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.