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