标签:put oid inter count imp down asn final interrupt
1、ArrayList线程不安全;CopyOnWriteArrayList线程安全import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* Auth: zhouhongliang
* Date:2019/8/1
* CopyOnWriteArrayList 替代 ArrayList
*/
public class CopyOnWriteArrayListDemo {
public static void main(String[] args) {
List<Integer> list = new CopyOnWriteArrayList<>();
//List<Integer> list = new CopyOnWriteArrayList<>();
for (int i=0;i<1000;i++){
list.add(i);
}
Iterator iterator = list.iterator();
while(iterator.hasNext()){
list.remove(iterator.next());
}
System.out.println(list);
}
}
2、HashMap线程不安全;HashTable synchronize线程安全、```
ConcurrentHashMap采用”分段锁”线程安全;
package concurrent;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
标签:put oid inter count imp down asn final interrupt
原文地址:https://blog.51cto.com/11147669/2425652