CopyOnWriteArrayList Sample Code
程序员文章站
2022-07-14 12:45:18
...
A simple one from http://www.ibm.com/developerworks/java/library/j-tiger06164.html.
import java.util.*;
import java.util.concurrent.*;
public class CopyOnWrite {
public static void main(String args[]) {
List list1 = new CopyOnWriteArrayList(Arrays.asList(args));
List list2 = new ArrayList(Arrays.asList(args));
Iterator itor1 = list1.iterator();
Iterator itor2 = list2.iterator();
list1.add("New");
list2.add("New");
try {
printAll(itor1);
} catch (ConcurrentModificationException e) {
System.err.println("Shouldn't get here");
}
try {
printAll(itor2);
} catch (ConcurrentModificationException e) {
System.err.println("Will get here.");
}
}
private static void printAll(Iterator itor) {
while (itor.hasNext()) {
System.out.println(itor.next());
}
}
}
A complex example using multiple threads.
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
public class MultiThreads {
public static void main(String[] args) throws Exception {
List<Val> ls = new CopyOnWriteArrayList<Val>();
ls.add(new Val(1, 10));
ls.add(new Val(2, 20));
ls.add(new Val(3, 30));
CountDownLatch latch = new CountDownLatch(1);
Walker w = new Walker(ls.iterator(), latch);
Modifer m = new Modifer(ls, latch);
w.start();
m.start();
latch.await();
System.out.println("===========");
Iterator<Val> it = ls.iterator();
while (it.hasNext())
System.out.println("[main] " + it.next());
}
}
class Walker extends Thread {
Iterator<Val> it;
CountDownLatch latch;
Walker(Iterator<Val> it, CountDownLatch latch) {
this.it = it;
this.latch = latch;
}
public void run() {
try {
latch.await();
System.out.println("===========");
while (it.hasNext())
System.out.println("[walker] " + it.next());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
class Modifer extends Thread {
List<Val> ls;
CountDownLatch latch;
public Modifer(List<Val> ls, CountDownLatch latch) {
this.ls = ls;
this.latch = latch;
}
public void run() {
try {
ls.set(0, new Val(1, 100));
ls.remove(2);
ls.add(new Val(3, 300));
ls.add(new Val(4, 400));
latch.countDown();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
class Val {
private int no;
private int age;
public Val(int no, int age) {
this.no = no;
this.age = age;
}
public String toString() {
return "no: " + no + ", age: " + age;
}
}
上一篇: GCC帧指针的开启与关闭以及反汇编测试
下一篇: GCC-O优化等级
推荐阅读
-
C#生成Code39条形码而非条形码字体的方法
-
解决Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-f8IeEI/MYSQL-python/
-
Entity Framework Code First属性映射约定
-
记录微信支付开发中的小经验(errcode = 40163; errmsg = "code been used")
-
利用Typings为Visual Studio Code实现智能提示功能
-
并发容器详解之CopyOnWriteArrayList
-
全新Visual Studio Code预览版0.10.10发布下载
-
Let's Code
-
ubuntu下VS code如何调试C++代码
-
详解VS Code使用之Vue工程配置format代码格式化