@@ -894,17 +894,23 @@ public SearchHistoryButton(@Nullable SearchControlFieldMode mode) {
894
894
JPopupMenu popupMenu = new JPopupMenu ();
895
895
popupMenu .add (miClearHistory );
896
896
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
+ }
906
909
}
907
910
}
911
+ finally {
912
+ historyList .getReadWriteLock ().readLock ().unlock ();
913
+ }
908
914
popupMenu .show (this , 0 , this .getHeight ());
909
915
});
910
916
@@ -926,7 +932,13 @@ private void loadHistory() {
926
932
List <String > entries = mapper .readValue (json , new TypeReference <>() {
927
933
});
928
934
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
+ }
930
942
}
931
943
}
932
944
} catch (JsonProcessingException ex ) {
@@ -937,8 +949,14 @@ private void loadHistory() {
937
949
private void saveHistory () {
938
950
ObjectMapper mapper = new ObjectMapper ();
939
951
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
+ }
942
960
} catch (JsonProcessingException e ) {
943
961
logger .error ("Failed to write search history" , e );
944
962
}
0 commit comments