Java concurrency container copyonwritearraylist usage scenarios and internal implementation analysis

Source: Internet
Author: User

Java Concurrency container copyonwritearraylist
Copyonwritearraylist
As the name implies, re-establish a new array when the array is changed

It is designed to be a solution to the problem in the use of a thread-safe container vector.
In the vector container, when you need to perform a compound operation
For example:
Code 1

classObservable {PrivateList<observer> observers=NewVector<observer>(); Public voidaddobserver () {...} Public voidremoveobserver () {...} Public voidNotify () {Iterator<Observer> itr=observers.iterator (); while(Itr.hasnext ()) {Observer Observer=Itr.next (); Observer.notify ();}return;}}

The implementation of the Notify method in observable is normal in a single thread, but in multi-threading, because the contents of the observers array may change during notify execution, the traversal is invalidated. Even if the observers itself is thread-safe, it doesn't work.

Usually solve this problem, you can use synchronous method or lock
Code 2

Class Observable {private list<observer> observers=new vector<observer> ();p ublic synchronized void Addobserver () {...} Public synchronized void Removeobserver () {...} The synchronization method public synchronized void Notify () {iterator<observer> Itr=observers.iterator (), while (Itr.hasnext ()) { Observer Observer=itr.next (); Observer.notify ();} return;}}

  

The synchronization of notify in such a solution leads to another problem, That is, the problem of activity. This method holds the lock for a long time when there are many elements in the observers, or notify method calls for each element. Causes any other behavior that you want to modify observers to block. Finally, it seriously affects program performance.

Copyonwritearraylist is used in this scenario. One needs to operate in multiple threads and traverse frequently.
It solves the performance problem caused by locking the entire array for a long time.
Its solution is a write-time copy.

Let's first put out the observable code that uses Copyonwritearraylist.
Code 3

classObservable {PrivateList<observer> observers=NewCopyonwritearraylist<observer>(); Public voidaddobserver () {...} Public voidremoveobserver () {...} Public voidNotify () {Iterator<Observer> itr=observers.iterator (); while(Itr.hasnext ()) {Observer Observer=Itr.next (); Observer.notify ();}return;}}

Observable's Notify method is the same as code 1. But it does not have multi-threaded simultaneous operation issues. The mystery, through the analysis of the source
When Copyonwritearraylist adds or deletes an element, it is now re-establishing a new array based on the current array.

Iterator<observer> Itr=observers.iterator ();


When we get an iterator for copyonwritearraylist, the reference to the current array is saved in the iterator. Then, if another thread changes the elements in the copyonwritearraylist, according to Copyonwritearraylist's characteristics, This iterator does not actually change the contents of the array.





Java concurrency container copyonwritearraylist usage scenarios and internal implementation 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.