Copyonwritearraylist Source Reading and analysis

Source: Internet
Author: User

Copyonwritearraylist source reading and analysis

Copyonwritearraylist is a thread-safe under concurrent packages that can achieve high concurrency ArrayList

First, let's look at how it's constructed:

final void SetArray (object[] a) {

Array = A;

}

/**

* Creates an empty list.

*/

Public Copyonwritearraylist () {

SetArray (new object[0]);

}

As you can see, it differs from ArrayList in that it creates an array of size 0. ArrayList is an empty array.

Let's take a look at its add (E) method to learn more about how it's implemented

As the name implies, it is called copyonwritearraylist (), that is, when the write operation, such as Add,remove, will be copied, that is, create a new 1 longer than the current number of new arrays, and then copy the old values past. The code looks like this:

Public boolean Add (E e) {

Final Reentrantlock lock = This.lock;

Lock.lock ();

try {

object[] elements = GetArray ();

int len = elements.length;

object[] newelements = arrays.copyof (elements, Len + 1);

Newelements[len] = e;

SetArray (newelements);

return true;

} finally {

Lock.unlock ();

}

}

The Add method does not use the Synchronized keyword for mutual exclusion, but instead uses Reentrantlock to lock the operation. As you can see, it is simple to implement, lock first, then create a new array that is 1 larger than the current array length, and then copy the values from the old array to the new array. Then call the SetArray method to alter the reference, complete the add operation, and then release the lock after the end.

Each time the array operation (Add/remove, etc.) is modified, a new array is created, and the original reference is then pointed to the new array after the modification is complete. is to ensure that when an array is traversed by one thread, no other threads are modified on the group. Therefore, it is not possible to throw concurrentmodificationexception errors that occur in ArrayList concurrency cases.

Let's take a look at the Get (int) method

Let's take a look at the implementation of the Code:

Public E get (int index) {

Return get (GetArray (), index);

}


@SuppressWarnings ("Unchecked")

Private E get (object[] A, int index) {

Return (E) A[index];

}

As you can see, it is very simple to get the element that corresponds to the current array directly, but because the method does not do any lock operation, it is possible to read dirty data, so it can have higher performance, so in less modification, read more scenes. It begins to be a good choice.

Take a look at the Remove (E) method

As with the Add method, this method is also reetrantlock to ensure its thread safety.

First, determine if the element you want to delete is the last element:

object[] elements = GetArray ();

int len = elements.length;

E OldValue = Get (elements, index);

int nummoved = len-index-1;

In the case of the last element, the original array is removed and the last element is copied to the new array, and then the reference is changed. As shown below:

if (nummoved = = 0)

SetArray (arrays.copyof (elements,len-1));

If it is not the last element, make two copies, first copy the original array from 0 to Index-1 to the new array, then copy the index+1 to the last element into the new array, and then change the reference. When this is done, the delete operation is completed.

You can see that copyonwritearraylist does not have a dynamic scaling mechanism like ArrayList, but instead creates a new array each time a log group is modified, in order to avoid concurrentmodificationexception exceptions, However, this can also have a certain impact on performance. And it does not perform any thread-safe operation on the read operation, so there may be a case of reading dirty data. So when the writing operation is relatively long, the use of copyonwritearraylist personal feeling is not a good choice.

Copyonwritearraylist Source Reading and analysis

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.