企业🤖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); } ~~~