Skip to content

Commit

Permalink
Fix #5862. It was indeed the throttler (at least it is working now fo…
Browse files Browse the repository at this point in the history
…r me)
  • Loading branch information
tobiasdiez committed Jan 25, 2020
1 parent ac90ad2 commit 37b7a40
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Boolean> selectedEntriesMatchStatus = EasyBind.map(stateManager.getSelectedEntries(), groupNode::matches);
anySelectedEntriesMatched = BindingsHelper.any(selectedEntriesMatchStatus, matched -> matched);
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/org/jabref/gui/util/CurrentThreadTaskExecutor.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -8,6 +9,8 @@

import javafx.concurrent.Task;

import org.jabref.logic.util.DelayTaskThrottler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -19,6 +22,7 @@
public class CurrentThreadTaskExecutor implements TaskExecutor {

private static final Logger LOGGER = LoggerFactory.getLogger(CurrentThreadTaskExecutor.class);
private final WeakHashMap<DelayTaskThrottler, Void> throttlers = new WeakHashMap<>();

/**
* Executes the task on the current thread. The code is essentially taken from {@link
Expand Down Expand Up @@ -60,7 +64,14 @@ public <V> Future<?> schedule(BackgroundTask<V> 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<T> implements Future<T> {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/jabref/gui/util/DefaultTaskExecutor.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -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<DelayTaskThrottler, Void> throttlers = new WeakHashMap<>();

/**
*
Expand Down Expand Up @@ -110,6 +114,14 @@ public <V> Future<?> schedule(BackgroundTask<V> 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 <V> Task<V> getJavaFXTask(BackgroundTask<V> task) {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/org/jabref/gui/util/TaskExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

0 comments on commit 37b7a40

Please sign in to comment.