Http://www.cnblogs.com/skywang12345/p/3498497.html?utm_source=tuicool
Summary
This chapter is the Copyonwritearrayset article in the Juc series . Next, we will first introduce the Copyonwritearrayset, then explain its rationale, and then through the code to analyze, and finally through the example to further understand Copyonwritearrayset. The content includes:
Copyonwritearrayset Introduction
Copyonwritearrayset principle and data structure
Copyonwritearrayset Function List
Copyonwritearrayset Source code (JDK1.7.0_40 version)
Copyonwritearrayset sample
Reprint please indicate the source: http://www.cnblogs.com/skywang12345/p/3498497.html
copyonwritearrayset Introduction
It is a thread-safe, unordered collection that can be interpreted as thread-safe hashset. Interestingly, both Copyonwritearrayset and hashset inherit from the common parent class Abstractset, but HashSet is implemented through the hash table (HASHMAP). Copyonwritearrayset is implemented through a "dynamic array (copyonwritearraylist)" and is not a hash table.
Similar to Copyonwritearraylist, Copyonwritearrayset has the following characteristics:
1. It is best suited for applications with the following characteristics: The Set size is usually kept small, read-only operations are much more than the variable operation, and there is a need to prevent conflicts between threads during traversal.
2. It is thread safe.
3. Because it is often necessary to replicate the entire base array, the overhead of a volatile operation (add (), set (), and remove (), and so on) is significant.
4. Iterators support Hasnext (), Next (), and other immutable operations, but do not support the variable remove () and other operations.
5. Iterating with iterators is fast and does not conflict with other threads. When constructing an iterator, the iterator relies on an invariant array snapshot.
Recommendation: Before learning Copyonwritearrayset, first through the "Java Collection Series 16 HashSet detailed introduction (source analysis) and use of examples" to hashset understanding.
copyonwritearrayset principle and data structure
The COPYONWRITEARRAYSET data structure, as shown in the following illustration:
Description :
1. Copyonwritearrayset inherits from Abstractset, which means it is a collection.
2. Copyonwritearrayset contains Copyonwritearraylist objects, which are implemented through copyonwritearraylist. And the Copyonwritearraylist essence is a dynamic array queue,
So Copyonwritearrayset is equivalent to a "set" implemented through a dynamic array. Duplicate elements are allowed in the copyonwritearraylist; however, Copyonwritearrayset is a collection, so it cannot have a duplicate collection. As a result, Copyonwritearraylist provides additional APIs for the addition of Addifabsent () and Addallabsent (), which add elements when they are added, only when the element does not exist.
As for Copyonwritearrayset, the "thread-safe" mechanism, like copyonwritearraylist, is implemented through volatile and mutexes. This is explained in the previous section when the Copyonwritearraylist data structure is described, and is not repeated here.
copyonwritearrayset Function List
//creates an empty set.
Copyonwritearrayset ()//Creates a set containing all elements of the specified collection.
Copyonwritearrayset (collection<? extends e> c)//If the specified element does not exist in this set, add it.
Boolean Add (E E)//If all elements in collection are not specified in this set, they are added to this set.
Boolean AddAll (collection<? extends e> C)//Remove all elements from this set.
void clear ()//If this set contains the specified element, returns TRUE.
Boolean contains (Object O)//If this set contains all elements of the specified collection, returns True.
Boolean containsall (collection<?> C)//compares the specified object to the equality of this set.
Boolean equals (Object O)//If this set does not contain any elements, returns true.
Boolean isempty ()//Returns an iterator that iterates over the elements contained in this set in the order in which they are added.
Iterator<e> iterator ()//If the specified element exists in this set, it is removed.
The Boolean remove (Object o)//Removes all elements in this set that are contained in the specified collection.
The Boolean removeall (collection<?> C)//only retains those elements in this set that are contained in the specified Collection.
Boolean retainall (collection<?> C)//Returns the number of elements in this set.
int size ()//Returns an array containing all the elements of this set.
Object[] ToArray ()//Returns an array containing all the elements of this set; the Run-time type of the array returns the type of the specified array. <T> t[] ToArray (t[] a)
Copyonwritearrayset Source code (jdk1.7.0_40 version)
Copyonwritearrayset.java's complete source code is as follows: View code
Copyonwritearrayset is implemented through Copyonwritearraylist, and its APIs are basically implemented by invoking the Copyonwritearraylist API. Believe to copyonwritearraylist understand words, to copyonwritearrayset understanding is the matter; So, here is no longer a detailed analysis of the Copyonwritearrayset code. If you do not understand the copyonwritearraylist, please refer to "Java Multithreaded Series-" Juc set "02 of Copyonwritearraylist".
Copyonwritearrayset Sample
Below, we use an example to compare hashset and Copyonwritearrayset.
Import java.util.*;
Import java.util.concurrent.*;
* * Copyonwritearrayset is a collection of "thread-safe", while HashSet is not thread-safe.
* * Below is an example of "multiple threads operating simultaneously and traversing set set" * (01) When the set is a Copyonwritearrayset object, the program works correctly.
* (02) When the set is a HashSet object, the program produces a concurrentmodificationexception exception.
* * @author Skywang/public class CopyOnWriteArraySetTest1 {//Todo:set is a HashSet object, the program will make an error.
private static set<string> Set = new hashset<string> ();
private static set<string> Set = new copyonwritearrayset<string> ();
public static void Main (string[] args) {//Start two threads at the same time to operate on the set.
New Mythread ("Ta"). Start ();
New Mythread ("TB"). Start ();
private static void Printall () {String value = null;
Iterator iter = Set.iterator ();
while (Iter.hasnext ()) {value = (String) iter.next ();
System.out.print (value+ ",");
} System.out.println (); private static Class Mythread extends Thread {MYthread (String name) {super (name);
@Override public void Run () {int i = 0; while (i++ < 10) {//"thread name" + "-" + "ordinal" String val = Thread.CurrentThread (). GetName ()
+ "-" + (i%6);
Set.add (Val);
Traverse set through "iterator".
Printall (); }
}
}
}
(one time) run results :
Ta-1, Tb-1, Ta-1, Tb-1, Ta-1, Tb-1, Ta-1, Ta-2, Tb-1, Ta-1, Ta-2, Tb-1, Tb-2, Ta-2, Ta-1, Tb-2, Tb-1, ta-3, Ta-2, ta- 1, Tb-2, Tb-1, ta-3, Ta-2, tb-3, Tb-2, Ta-1, ta-3, Tb-1, tb-3, Ta-2, ta-4, Tb-2, Ta-1, ta-3, Tb-1, tb-3, Ta-2, ta-4, tb- 2, Tb-4, ta-3, Ta-1, tb-3, Tb-1, ta-4, Ta-2, tb-4, Tb-2, ta-5, ta-3, Ta-1, tb-3, Tb-1, ta-4, Ta-2, tb-4, Tb-2, ta-5, ta- 3, Tb-5, tb-3, Ta-1, ta-4, Tb-1, tb-4, Ta-2, ta-5, Tb-2, tb-5, ta-3, ta-0, tb-3, Ta-1, ta-4, Tb-1, tb-4, Ta-2, ta-5, tb- 2, Tb-5, ta-3, ta-0, tb-3, tb-0, ta-4, Ta-1, tb-4, Tb-1, ta-5, Ta-2, tb-5, Tb-2, ta-0, ta-3, tb-0, tb-3, Ta-1, ta-4, tb- 1, tb-4, Ta-2, ta-5, tb-5, ta-0, tb-0, Ta-1, Tb-2, Tb-1, ta-3, Ta-2, tb-3, Tb-2, ta-4, ta-3, tb-4, tb-3, ta-5, ta-4, tb-5 , Tb-4, ta-0, ta-5, tb-0, tb-5, Ta-1, ta-0, Tb-1, tb-0, Ta-2, Ta-1, Tb-2, Tb-1, ta-3, Ta-2, tb-3, Tb-2, ta-4, ta-3, tb-4 , Tb-3, Ta-5, tb-5, ta-0, tb-0, ta-4, Ta-1, tb-4, Tb-1, ta-5, Ta-2, tb-5, Tb-2, ta-0, ta-3, tb-0, tb-3, Ta-1, ta-4, tb-1 , Tb-4, Ta-2, ta-5, tb-2, Tb-5, Ta-3, ta-0, tb-3, tb-0, ta-4, tb-4, ta-5, tb-5, ta-0, tb-0,
Results show :
Because set is a collection object, it does not contain duplicate elements.
If you change the set in the source code to a HashSet object, the program produces a concurrentmodificationexception exception.
more Content
1. Java Multithreading Series--"Juc set" 01 of the framework
2. Java Multithreading Series--"Juc set" 02 Copyonwritearraylis
3. Java multithread Series catalogue (total XX articles)
4. Java Collection Series catalog (Category)