commit
67fc67e1fa
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -0,0 +1 @@
|
|||
.DS_Store
|
|
@ -255,6 +255,35 @@ List<String> synList = Collections.synchronizedList(list);
|
|||
List<String> list = new CopyOnWriteArrayList<>();
|
||||
```
|
||||
|
||||
CopyOnWriteArrayList是一种CopyOnWrite容器,从以下源码看出:读取元素是从原数组读取;添加元素是在复制的新数组上。读写分离,因而可以在并发条件下进行不加锁的读取,读取效率高,适用于读操作远大于写操作的场景。
|
||||
|
||||
```java
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
final void setArray(Object[] a) {
|
||||
array = a;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private E get(Object[] a, int index) {
|
||||
return (E) a[index];
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
## LinkedList
|
||||
|
||||
### 1. 概览
|
||||
|
|
Loading…
Reference in New Issue
Block a user