Skip to content

Commit a7d4df1

Browse files
committed
- add locks around event list handling
1 parent 9a547c7 commit a7d4df1

File tree

2 files changed

+39
-13
lines changed

2 files changed

+39
-13
lines changed

src/main/java/mediathek/gui/tabs/tab_film/EditHistoryDialog.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,15 @@ public void windowClosed(WindowEvent e) {
5454
for (var idx : list.getSelectedIndices()) {
5555
changeList.add(list.getModel().getElementAt(idx));
5656
}
57-
changeList.forEach(eventList::remove);
57+
58+
var lock = eventList.getReadWriteLock().writeLock();
59+
lock.lock();
60+
try {
61+
changeList.forEach(eventList::remove);
62+
}
63+
finally {
64+
lock.unlock();
65+
}
5866
changeList.clear();
5967
});
6068

src/main/java/mediathek/gui/tabs/tab_film/GuiFilme.java

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -894,17 +894,23 @@ public SearchHistoryButton(@Nullable SearchControlFieldMode mode) {
894894
JPopupMenu popupMenu = new JPopupMenu();
895895
popupMenu.add(miClearHistory);
896896
popupMenu.add(miEditHistory);
897-
if (!historyList.isEmpty()) {
898-
popupMenu.addSeparator();
899-
for (var item : historyList) {
900-
JMenuItem historyItem = new JMenuItem(item);
901-
historyItem.addActionListener(li -> {
902-
searchField.setText(item);
903-
searchField.fireActionPerformed();
904-
});
905-
popupMenu.add(historyItem);
897+
historyList.getReadWriteLock().readLock().lock();
898+
try {
899+
if (!historyList.isEmpty()) {
900+
popupMenu.addSeparator();
901+
for (var item : historyList) {
902+
JMenuItem historyItem = new JMenuItem(item);
903+
historyItem.addActionListener(li -> {
904+
searchField.setText(item);
905+
searchField.fireActionPerformed();
906+
});
907+
popupMenu.add(historyItem);
908+
}
906909
}
907910
}
911+
finally {
912+
historyList.getReadWriteLock().readLock().unlock();
913+
}
908914
popupMenu.show(this, 0, this.getHeight());
909915
});
910916

@@ -926,7 +932,13 @@ private void loadHistory() {
926932
List<String> entries = mapper.readValue(json, new TypeReference<>() {
927933
});
928934
if (!entries.isEmpty()) {
929-
historyList.addAll(entries);
935+
historyList.getReadWriteLock().writeLock().lock();
936+
try {
937+
historyList.addAll(entries);
938+
}
939+
finally {
940+
historyList.getReadWriteLock().writeLock().unlock();
941+
}
930942
}
931943
}
932944
} catch (JsonProcessingException ex) {
@@ -937,8 +949,14 @@ private void loadHistory() {
937949
private void saveHistory() {
938950
ObjectMapper mapper = new ObjectMapper();
939951
try {
940-
var json = mapper.writeValueAsString(historyList);
941-
ApplicationConfiguration.getConfiguration().setProperty(SEARCH_HISTORY_CONFIG, json);
952+
historyList.getReadWriteLock().readLock().lock();
953+
try {
954+
var json = mapper.writeValueAsString(historyList);
955+
ApplicationConfiguration.getConfiguration().setProperty(SEARCH_HISTORY_CONFIG, json);
956+
}
957+
finally {
958+
historyList.getReadWriteLock().readLock().unlock();
959+
}
942960
} catch (JsonProcessingException e) {
943961
logger.error("Failed to write search history", e);
944962
}

0 commit comments

Comments
 (0)