From 6b32632e1449f162f4c1d8c43730875bee3de35b Mon Sep 17 00:00:00 2001 From: pajlada Date: Sun, 24 Nov 2024 21:40:43 +0100 Subject: [PATCH] fix: synchronize questStepPanelList (#1866) * fix: synchronize questStepPanelList Since we create/add to it from AWT thread but have to iterate & access its stuff from client thread, this seems like a reasonable solution * fix: clear questStepPanelList before adding panels from a new quest Before this, we kept adding and adding more panels, making the iteration over steps slower and slower * Make use of CopyOnWriteArray instead This is automatically safe for iterations (which Collections.synchronizedList is not) and it makes more sense for our use case given the rarity of our writes --- src/main/java/com/questhelper/panel/QuestOverviewPanel.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/questhelper/panel/QuestOverviewPanel.java b/src/main/java/com/questhelper/panel/QuestOverviewPanel.java index a23a771c2c..c491cfc6af 100644 --- a/src/main/java/com/questhelper/panel/QuestOverviewPanel.java +++ b/src/main/java/com/questhelper/panel/QuestOverviewPanel.java @@ -40,6 +40,7 @@ import java.awt.event.ItemEvent; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; +import java.util.concurrent.CopyOnWriteArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -94,7 +95,7 @@ public class QuestOverviewPanel extends JPanel private final JButton collapseBtn = new JButton(); - private final List questStepPanelList = new ArrayList<>(); + private final List questStepPanelList = new CopyOnWriteArrayList<>(); public QuestOverviewPanel(QuestHelperPlugin questHelperPlugin, QuestManager questManager) { @@ -254,6 +255,7 @@ private JPanel makeDropdownPanel(JComboBox dropdown, String name) public void addQuest(QuestHelper quest, boolean isActive) { currentQuest = quest; + questStepPanelList.clear(); List steps = quest.getPanels(); QuestStep currentStep;