From 92cc1934eafa9c9489a3cb29828b6ee3e541c1f9 Mon Sep 17 00:00:00 2001 From: bryan31 Date: Tue, 18 Jan 2022 23:52:24 +0800 Subject: [PATCH] =?UTF-8?q?enhancement=20#I4QV69=20=E5=8A=A0=E8=BD=BD?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E8=84=9A=E6=9C=AC=E6=9C=89=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E5=AE=89=E5=85=A8=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../liteflow/util/CopyOnWriteHashMap.java | 32 +++++++++---------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/util/CopyOnWriteHashMap.java b/liteflow-core/src/main/java/com/yomahub/liteflow/util/CopyOnWriteHashMap.java index 265cb894b..e24dc4bf6 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/util/CopyOnWriteHashMap.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/util/CopyOnWriteHashMap.java @@ -45,6 +45,8 @@ import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; import java.util.concurrent.atomic.AtomicBoolean; /** @@ -56,27 +58,25 @@ import java.util.concurrent.atomic.AtomicBoolean; * * @author Paul.Sandoz@Oracle.Com * @author pavel.bucek@oracle.com + * @author Bryan.Zhang */ -public class CopyOnWriteHashMap implements Map { - volatile Map view; +public class CopyOnWriteHashMap extends ConcurrentHashMap { - private Map duplicate(Map original) { - Map result = new HashMap(); + volatile ConcurrentHashMap view; + + private ConcurrentHashMap duplicate(ConcurrentHashMap original) { // SUBTLETY: note that original.entrySet() grabs the entire contents of the original Map in a // single call. This means that if the original map is Thread-safe or another CopyOnWriteHashMap, // we can safely iterate over the list of entries. - for (Map.Entry entry : original.entrySet()) { - result.put(entry.getKey(), entry.getValue()); - } - return result; + return new ConcurrentHashMap<>(original); } - public CopyOnWriteHashMap(Map that) { + public CopyOnWriteHashMap(ConcurrentHashMap that) { this.view = duplicate(that); } public CopyOnWriteHashMap() { - this(new HashMap()); + this(new ConcurrentHashMap<>()); } @Override @@ -114,7 +114,7 @@ public class CopyOnWriteHashMap implements Map { } @Override - public Set keySet() { + public KeySetView keySet() { return view.keySet(); } @@ -149,7 +149,7 @@ public class CopyOnWriteHashMap implements Map { @Override public V put(K key, V value) { synchronized (this) { - Map newCore = duplicate(view); + ConcurrentHashMap newCore = duplicate(view); V result = newCore.put(key, value); view = newCore; // volatile write return result; @@ -159,18 +159,17 @@ public class CopyOnWriteHashMap implements Map { @Override public V remove(Object key) { synchronized (this) { - Map newCore = duplicate(view); + ConcurrentHashMap newCore = duplicate(view); V result = newCore.remove(key); view = newCore; // volatile write return result; - } } @Override public void putAll(Map t) { synchronized (this) { - Map newCore = duplicate(view); + ConcurrentHashMap newCore = duplicate(view); newCore.putAll(t); view = newCore; // volatile write } @@ -179,8 +178,7 @@ public class CopyOnWriteHashMap implements Map { @Override public void clear() { synchronized (this) { - Map newCore = new HashMap(); - view = newCore; // volatile write + view = new ConcurrentHashMap<>(); // volatile write } } } \ No newline at end of file