标签:
CopyOnWriteArrayList原理
首先每次写操作,都将数组copy一份,并赋值给arrays
读操作读不加锁
写操作加锁
ReentrantLock
因为每次写都要copy数组,这是一项繁重的操作,因此
CopyOnWriteArrayList适合读多写少的情况。
add方法的源码如下:
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(); } }
参考文章:
http://blog.csdn.net/wjwj1203/article/details/8109000
http://www.cnblogs.com/sunwei2012/archive/2010/10/08/1845656.html
标签:
原文地址:http://www.cnblogs.com/tangyanbo/p/4554140.html