From f23c5eee17b2bc4d380b230d0bf5d79c138704f1 Mon Sep 17 00:00:00 2001 From: quangz Date: Sat, 2 Apr 2022 08:06:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DConcurrentModificationExcepti?= =?UTF-8?q?on?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../vmp/media/zlm/ZLMHttpHookSubscribe.java | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMHttpHookSubscribe.java b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMHttpHookSubscribe.java index 35052253..f7551c23 100644 --- a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMHttpHookSubscribe.java +++ b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMHttpHookSubscribe.java @@ -3,6 +3,7 @@ package com.genersoft.iot.vmp.media.zlm; import com.alibaba.fastjson.JSONObject; import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem; import org.springframework.stereotype.Component; +import org.springframework.util.CollectionUtils; import java.util.*; import java.util.concurrent.ConcurrentHashMap; @@ -39,12 +40,8 @@ public class ZLMHttpHookSubscribe { private Map> allSubscribes = new ConcurrentHashMap<>(); public void addSubscribe(HookType type, JSONObject hookResponse, ZLMHttpHookSubscribe.Event event) { - Map eventMap = allSubscribes.get(type); - if (eventMap == null) { - eventMap = new HashMap(); - allSubscribes.put(type,eventMap); - } - eventMap.put(hookResponse, event); + allSubscribes.computeIfAbsent(type, k -> new ConcurrentHashMap<>()) + .put(hookResponse, event); } public ZLMHttpHookSubscribe.Event getSubscribe(HookType type, JSONObject hookResponse) { @@ -81,7 +78,8 @@ public class ZLMHttpHookSubscribe { Set> entries = eventMap.entrySet(); if (entries.size() > 0) { - for (Map.Entry entry : entries) { + List> entriesToRemove = new ArrayList<>(); + for (Map.Entry entry : entries) { JSONObject key = entry.getKey(); Boolean result = null; for (String s : key.keySet()) { @@ -93,9 +91,16 @@ public class ZLMHttpHookSubscribe { } } if (null != result && result){ + entriesToRemove.add(entry); + } + } + + if (!CollectionUtils.isEmpty(entriesToRemove)) { + for (Map.Entry entry : entriesToRemove) { entries.remove(entry); } } + } }