🤲 List线程安全版本Vector
一个线程安全的List版本
🙆♂️原理
底层方法基本都添加了synchronized
,比如下面两个常见的方法。
public synchronized void addElement(E obj) {
++this.modCount;
this.add(obj, this.elementData, this.elementCount);
}
public synchronized boolean removeElement(Object obj) {
++this.modCount;
int i = this.indexOf(obj);
if (i >= 0) {
this.removeElementAt(i);
return true;
} else {
return false;
}
}
🍐 Map 的线程安全版本
参见这篇文章:HashMap 与 ConcurrentHashMap 的实现原理
字数:128
发布于 1 个月前