标签:tor 一个 效率 sub val tran 移除 ext turn
//java.util.ConcurrentModificationException 并发修改异常
public class Test03 {
public static void main(String[] args) {
/*并发下的ArrayList 是不安全的
* */
ArrayList<String> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
new Thread(()->{
list.add(UUID.randomUUID().toString().substring(0,5));
System.out.println(list);
},String.valueOf(i)).start();
}
}
}
//Exception in thread "2" Exception in thread "4" Exception in thread "3" Exception in thread "5" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
at java.util.ArrayList$Itr.next(ArrayList.java:851)
at java.util.AbstractCollection.toString(AbstractCollection.java:461)
at java.lang.String.valueOf(String.java:2981)
at java.io.PrintStream.println(PrintStream.java:821)
at com.saxon.demo.Test03.lambda$main$0(Test03.java:22)
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
public synchronized boolean add(E e) {
modCount++;
ensureCapacityHelper(elementCount + 1);
elementData[elementCount++] = e;
return true;
}
区别:就加了个synchronized
那JDK为什么不在ArrayList的add加上synchronized呢
并且Vector的出现比ArrayList要早
为了考虑效率问题所以去掉了synchronized
//java.util.ConcurrentModificationException 并发修改异常
public class Test03 {
public static void main(String[] args) {
/*并发下的ArrayList 是不安全的
* 解决方案:
* 1、Vector<String> list = new Vector<>(); 不常用
* 2、List<String> list = Collections.synchronizedList(new ArrayList<>());
* 3. List<String> list = new CopyOnWriteArrayList<>(); 推荐
* */
// ArrayList<String> list = new ArrayList<>();
// Vector<String> list = new Vector<>();
// List<String> list = Collections.synchronizedList(new ArrayList<>());
/* CopyOnWrite 写入复制 COW 计算初程序设领域的一种优化策略
* 多个线程调用的时候,List读取的时候,固定的,写入(覆盖)
* 在写入的时候避覆盖,造成数据问题
* CopyOnWriteArrayList比Vector厉害在哪里 看源码*/
List<String> list = new CopyOnWriteArrayList<>();
for (int i = 0; i < 10; i++) {
new Thread(()->{
list.add(UUID.randomUUID().toString().substring(0,5));
System.out.println(list);
},String.valueOf(i)).start();
}
}
}
public boolean add(E e) {
final ReentrantLock lock = this.lock;
lock.lock();
try {
Object[] elements = getArray();
int len = elements.length;
Object[] newElements = Arrays.copyOf(elements, len + 1);
newElements[len] = e;
setArray(newElements);
return true;
} finally {
lock.unlock();
}
}
CopyOnWriteArrayList比Vector厉害在哪里?
标签:tor 一个 效率 sub val tran 移除 ext turn
原文地址:https://www.cnblogs.com/saxonsong/p/14728054.html