Skip to content

Commit 6442ac9

Browse files
committed
Add throttle to AutosaveUIManager (fixes JabRef#5679)
1 parent 1c1550e commit 6442ac9

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
2424
- We added support to switch between biblatex and bibtex library types. [#5550](https://github.com/JabRef/jabref/issues/5550)
2525
- We changed the save action buttons to be easier to understand. [#5565](https://github.com/JabRef/jabref/issues/5565)
2626
- We made the columns for groups, files and uri in the main table reorderable and merged the clickable icon columns for uri, url, doi and eprint. [#5544](https://github.com/JabRef/jabref/pull/5544)
27+
- We reduced the number of write actions performed when autosave is enabled [#5679](https://github.com/JabRef/jabref/issues/5679)
2728

2829
### Fixed
2930

src/main/java/org/jabref/logic/autosaveandbackup/AutosaveManager.java

+12-16
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@
22

33
import java.util.HashSet;
44
import java.util.Set;
5-
import java.util.concurrent.ArrayBlockingQueue;
6-
import java.util.concurrent.BlockingQueue;
7-
import java.util.concurrent.ExecutorService;
8-
import java.util.concurrent.RejectedExecutionException;
9-
import java.util.concurrent.ThreadPoolExecutor;
10-
import java.util.concurrent.TimeUnit;
5+
import java.util.concurrent.*;
116

127
import org.jabref.model.database.BibDatabaseContext;
138
import org.jabref.model.database.event.AutosaveEvent;
@@ -26,33 +21,34 @@
2621
public class AutosaveManager {
2722

2823
private static final Logger LOGGER = LoggerFactory.getLogger(AutosaveManager.class);
24+
private static final int AUTO_SAVE_DELAY = 200;
2925

3026
private static Set<AutosaveManager> runningInstances = new HashSet<>();
3127

3228
private final BibDatabaseContext bibDatabaseContext;
33-
private final BlockingQueue<Runnable> workerQueue;
34-
private final ExecutorService executor;
29+
private final ScheduledExecutorService executor;
3530
private final EventBus eventBus;
3631
private final CoarseChangeFilter changeFilter;
32+
private Future<?> scheduledSaveAction;
3733

3834
private AutosaveManager(BibDatabaseContext bibDatabaseContext) {
3935
this.bibDatabaseContext = bibDatabaseContext;
40-
this.workerQueue = new ArrayBlockingQueue<>(1);
41-
this.executor = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS, workerQueue);
36+
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
37+
executor.setRemoveOnCancelPolicy(true); // This prevents memory leaks
38+
this.executor = executor;
4239
this.eventBus = new EventBus();
4340
this.changeFilter = new CoarseChangeFilter(bibDatabaseContext);
4441
changeFilter.registerListener(this);
4542
}
4643

4744
@Subscribe
4845
public synchronized void listen(@SuppressWarnings("unused") BibDatabaseContextChangedEvent event) {
49-
try {
50-
executor.submit(() -> {
51-
eventBus.post(new AutosaveEvent());
52-
});
53-
} catch (RejectedExecutionException e) {
54-
LOGGER.debug("Rejecting autosave while another save process is already running.");
46+
if(scheduledSaveAction != null) {
47+
scheduledSaveAction.cancel(false);
5548
}
49+
scheduledSaveAction = executor.schedule(() -> {
50+
eventBus.post(new AutosaveEvent());
51+
}, AUTO_SAVE_DELAY, TimeUnit.MILLISECONDS);
5652
}
5753

5854
private void shutdown() {

0 commit comments

Comments
 (0)