diff --git a/src/main/java/org/jabref/gui/groups/GroupNodeViewModel.java b/src/main/java/org/jabref/gui/groups/GroupNodeViewModel.java index af10541617f..e8cb54656c7 100644 --- a/src/main/java/org/jabref/gui/groups/GroupNodeViewModel.java +++ b/src/main/java/org/jabref/gui/groups/GroupNodeViewModel.java @@ -90,7 +90,7 @@ public GroupNodeViewModel(BibDatabaseContext databaseContext, StateManager state // The wrapper created by the FXCollections will set a weak listener on the wrapped list. This weak listener gets garbage collected. Hence, we need to maintain a reference to this list. entriesList = databaseContext.getDatabase().getEntries(); entriesList.addListener(this::onDatabaseChanged); - throttler = new DelayTaskThrottler(1000); + throttler = taskExecutor.createThrottler(1000); ObservableList selectedEntriesMatchStatus = EasyBind.map(stateManager.getSelectedEntries(), groupNode::matches); anySelectedEntriesMatched = BindingsHelper.any(selectedEntriesMatchStatus, matched -> matched); diff --git a/src/main/java/org/jabref/gui/util/CurrentThreadTaskExecutor.java b/src/main/java/org/jabref/gui/util/CurrentThreadTaskExecutor.java index ff7ef256068..a5f8ec393a1 100644 --- a/src/main/java/org/jabref/gui/util/CurrentThreadTaskExecutor.java +++ b/src/main/java/org/jabref/gui/util/CurrentThreadTaskExecutor.java @@ -1,5 +1,6 @@ package org.jabref.gui.util; +import java.util.WeakHashMap; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; @@ -8,6 +9,8 @@ import javafx.concurrent.Task; +import org.jabref.logic.util.DelayTaskThrottler; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -19,6 +22,7 @@ public class CurrentThreadTaskExecutor implements TaskExecutor { private static final Logger LOGGER = LoggerFactory.getLogger(CurrentThreadTaskExecutor.class); + private final WeakHashMap throttlers = new WeakHashMap<>(); /** * Executes the task on the current thread. The code is essentially taken from {@link @@ -60,7 +64,14 @@ public Future schedule(BackgroundTask task, long delay, TimeUnit unit) @Override public void shutdown() { - // Nothing to do here + throttlers.forEach((throttler, aVoid) -> throttler.shutdown()); + } + + @Override + public DelayTaskThrottler createThrottler(int delay) { + DelayTaskThrottler throttler = new DelayTaskThrottler(delay); + throttlers.put(throttler, null); + return throttler; } private class FailedFuture implements Future { diff --git a/src/main/java/org/jabref/gui/util/DefaultTaskExecutor.java b/src/main/java/org/jabref/gui/util/DefaultTaskExecutor.java index a59b068f7aa..91bf5da2180 100644 --- a/src/main/java/org/jabref/gui/util/DefaultTaskExecutor.java +++ b/src/main/java/org/jabref/gui/util/DefaultTaskExecutor.java @@ -1,6 +1,7 @@ package org.jabref.gui.util; import java.util.Objects; +import java.util.WeakHashMap; import java.util.concurrent.Callable; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; @@ -15,6 +16,8 @@ import javafx.application.Platform; import javafx.concurrent.Task; +import org.jabref.logic.util.DelayTaskThrottler; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -28,6 +31,7 @@ public class DefaultTaskExecutor implements TaskExecutor { private final ExecutorService executor = Executors.newFixedThreadPool(5); private final ScheduledExecutorService scheduledExecutor = Executors.newScheduledThreadPool(2); + private final WeakHashMap throttlers = new WeakHashMap<>(); /** * @@ -110,6 +114,14 @@ public Future schedule(BackgroundTask task, long delay, TimeUnit unit) public void shutdown() { executor.shutdownNow(); scheduledExecutor.shutdownNow(); + throttlers.forEach((throttler, aVoid) -> throttler.shutdown()); + } + + @Override + public DelayTaskThrottler createThrottler(int delay) { + DelayTaskThrottler throttler = new DelayTaskThrottler(delay); + throttlers.put(throttler, null); + return throttler; } private Task getJavaFXTask(BackgroundTask task) { diff --git a/src/main/java/org/jabref/gui/util/TaskExecutor.java b/src/main/java/org/jabref/gui/util/TaskExecutor.java index ec09e993f99..f56d516eed6 100644 --- a/src/main/java/org/jabref/gui/util/TaskExecutor.java +++ b/src/main/java/org/jabref/gui/util/TaskExecutor.java @@ -5,6 +5,8 @@ import javafx.concurrent.Task; +import org.jabref.logic.util.DelayTaskThrottler; + /** * An object that executes submitted {@link Task}s. This * interface provides a way of decoupling task submission from the @@ -46,4 +48,9 @@ public interface TaskExecutor { * Shutdown the task executor. */ void shutdown(); + + /** + * Creates a new task throttler, and registers it so that it gets properly shutdown. + */ + DelayTaskThrottler createThrottler(int delay); }