Java concurrent-copyonwritearraylist

Source: Internet
Author: User

Read copyonwritearraylist

In addition to locking, there is actually another way to prevent concurrent modification exceptions. This is the read/write splitting technology (not on the database ).

Let's look back at the next common sense:

1. in Java, the "=" operation only associates a reference with an object. If a thread points a reference to another object, a thread obtains the object pointed to by the reference, so there will be no concurrentmodificationexception between them. They are blocked at the virtual machine level, and the speed is very fast and almost no CPU time is required.

2. Two different references in Java point to the same object. When the first reference points to another object, the second reference will keep the original object.

 

Based on the above knowledge, let's discuss the following question:

To process write operations (including add, remove, and set) in copyonwritearraylist, the original data is first generated into a new array through arrays. copyof () of jdk1.6.

Write data on the new data object, and point the original reference to the current data object after writing the Data Object (common sense 1 is applied here ), this ensures that each write operation is performed on a new object (because of the consistency of write operations, a lock is required for various write operations, and jdk1.6 uses a re-entry lock here ),

Then, the system reads the referenced current object (including get and iterator) without locking or blocking. For iterator, a castrated version iterator called cowiterator is used, because write operations are not supported, when you get the iterator of copyonwritearraylist, the data reference in the iterator is directed to the Data Object pointed to by the current reference, no matter what future write operations, the data object reference in the iterator will not be changed any more, so the iterator is safe (Common Sense 2 is applied here ).

The write operation in copyonwritearraylist needs to copy an array in a large area, so the performance is definitely poor. However, because the operation object and the write operation are not the same object, no locks are required between the read operations, the synchronization process between read and write operations only points the reference to a new array object through a simple "=" after writing, which almost does not require time, in this way, the read operation is very fast and safe. It is suitable for multi-threaded use, and concurrentmodificationexception will never occur. Therefore, it is concluded that copyonwritearraylist is suitable for scenarios where the read operation is much larger than the write operation, such as caching.

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.