Collections.synchronizedMap和 Collections.synchronizedList
1. 代理map或者list
2. 每个方法都带锁
~~~
private static class SynchronizedMap<K, V> implements Map<K, V>, Serializable {
private static final long serialVersionUID = 1978198479659022715L;
private final Map<K, V> m;
final Object mutex;
private transient Set<K> keySet;
private transient Set<Entry<K, V>> entrySet;
private transient Collection<V> values;
SynchronizedMap(Map<K, V> var1) {
this.m = (Map)Objects.requireNonNull(var1);
this.mutex = this;
}
SynchronizedMap(Map<K, V> var1, Object var2) {
this.m = var1;
this.mutex = var2;
}
public int size() {
Object var1 = this.mutex;
synchronized(this.mutex) {
return this.m.size();
}
}
public boolean isEmpty() {
Object var1 = this.mutex;
synchronized(this.mutex) {
return this.m.isEmpty();
}
}
....
}
static class SynchronizedList<E> extends Collections.SynchronizedCollection<E> implements List<E> {
private static final long serialVersionUID = -7754090372962971524L;
final List<E> list;
SynchronizedList(List<E> var1) {
super(var1);
this.list = var1;
}
SynchronizedList(List<E> var1, Object var2) {
super(var1, var2);
this.list = var1;
}
public boolean equals(Object var1) {
if (this == var1) {
return true;
} else {
Object var2 = this.mutex;
synchronized(this.mutex) {
return this.list.equals(var1);
}
}
}
public int hashCode() {
Object var1 = this.mutex;
synchronized(this.mutex) {
return this.list.hashCode();
}
}
public E get(int var1) {
Object var2 = this.mutex;
synchronized(this.mutex) {
return this.list.get(var1);
}
~~~
- Java
- Object
- 内部类
- 异常
- 注解
- 反射
- 静态代理与动态代理
- 泛型
- 继承
- JVM
- ClassLoader
- String
- 数据结构
- Java集合类
- ArrayList
- LinkedList
- HashSet
- TreeSet
- HashMap
- TreeMap
- HashTable
- 并发集合类
- Collections
- CopyOnWriteArrayList
- ConcurrentHashMap
- Android集合类
- SparseArray
- ArrayMap
- 算法
- 排序
- 常用算法
- LeetCode
- 二叉树遍历
- 剑指
- 数据结构、算法和数据操作
- 高质量的代码
- 解决问题的思路
- 优化时间和空间效率
- 面试中的各项能力
- 算法心得
- 并发
- Thread
- 锁
- java内存模型
- CAS
- 原子类Atomic
- volatile
- synchronized
- Object.wait-notify
- Lock
- Lock之AQS
- Lock子类
- 锁小结
- 堵塞队列
- 生产者消费者模型
- 线程池