ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
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); } ~~~