-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathConcurrentArrayList.java
More file actions
46 lines (35 loc) · 1.73 KB
/
Copy pathConcurrentArrayList.java
File metadata and controls
46 lines (35 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package collection.demo;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;
public class ConcurrentArrayList {
public static void main(String args[]) {
String[] input = { "today is friday abc",
"happy friday",
"he is not coming abc to office"};
String search = "friday"; //he is not //is abc //xyz
CopyOnWriteArrayList<String> threadSafeList = new CopyOnWriteArrayList<String>();
threadSafeList.add("Java");
threadSafeList.add("J2EE");
threadSafeList.add("Collection");
//add, remove operator is not supported by CopyOnWriteArrayList iterator
Iterator<String> failSafeIterator = threadSafeList.iterator();
while(failSafeIterator.hasNext()){
System.out.printf("Read from CopyOnWriteArrayList : %s %n", failSafeIterator.next());
// failSafeIterator.remove(); //not supported in CopyOnWriteArrayList in Java
threadSafeList.add("creep");
// threadSafeList.remove(failSafeIterator.next());
}
ArrayList<Integer> a = new ArrayList<Integer>();
a.add(10);
a.add(20);
Iterator<Integer> aIter = a.iterator();
while(failSafeIterator.hasNext()){
System.out.printf("Read from CopyOnWriteArrayList : %s %n", failSafeIterator.next());
aIter.remove(); //not supported in CopyOnWriteArrayList in Java
// threadSafeList.add("creep");
// threadSafeList.remove(failSafeIterator.next());
}
}
}