Skip to content

Commit 2086ff8

Browse files
committed
Avoid race condition when reloading a session file.
1 parent 748f06f commit 2086ff8

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/model/io/SessionModelIO.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.List;
66
import java.util.Map;
77
import java.util.Set;
8+
import java.util.concurrent.ForkJoinPool;
89

910
import org.baderlab.csplugins.enrichmentmap.ApplicationModule.Headless;
1011
import org.baderlab.csplugins.enrichmentmap.CyActivator;
@@ -29,6 +30,7 @@
2930
import org.cytoscape.service.util.CyServiceRegistrar;
3031
import org.cytoscape.session.CySession;
3132

33+
import com.google.common.util.concurrent.ListenableFuture;
3234
import com.google.inject.Inject;
3335
import com.google.inject.Provider;
3436
import com.google.inject.Singleton;
@@ -101,7 +103,7 @@ public void restoreModel(CySession session) {
101103

102104
emManager.reset();
103105

104-
boolean sessionHasEM = false;
106+
boolean sessionHasEM;
105107
if(session != null && LegacySessionLoader.isLegacy(session)) {
106108
sessionHasEM = true;
107109
legacySessionLoaderProvider.get().loadSession(session);
@@ -112,12 +114,14 @@ public void restoreModel(CySession session) {
112114
if(!headless) {
113115
ControlPanelMediator controlPanelMediator = controlPanelMediatorProvider.get();
114116
HeatMapMediator heatMapMediator = heatMapMediatorProvider.get();
115-
controlPanelMediator.reset();
116-
heatMapMediator.reset();
117-
if(sessionHasEM) {
118-
controlPanelMediator.showControlPanel();
119-
heatMapMediator.showHeatMapPanel();
120-
}
117+
ListenableFuture<Void> future = controlPanelMediator.reset();
118+
future.addListener(() -> {
119+
heatMapMediator.reset();
120+
if(sessionHasEM) {
121+
controlPanelMediator.showControlPanel();
122+
heatMapMediator.showHeatMapPanel();
123+
}
124+
}, ForkJoinPool.commonPool());
121125
}
122126

123127
if(debug) {

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/view/util/SwingUtil.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.nio.file.Files;
1818
import java.nio.file.InvalidPathException;
1919
import java.nio.file.Paths;
20+
import java.util.concurrent.Callable;
2021

2122
import javax.annotation.Nullable;
2223
import javax.swing.AbstractButton;
@@ -44,6 +45,8 @@
4445
import org.cytoscape.util.swing.LookAndFeelUtil;
4546

4647
import com.google.common.base.Strings;
48+
import com.google.common.util.concurrent.ListenableFuture;
49+
import com.google.common.util.concurrent.ListenableFutureTask;
4750

4851

4952
public class SwingUtil {
@@ -160,6 +163,20 @@ public static void invokeOnEDT(final Runnable runnable) {
160163
SwingUtilities.invokeLater(runnable);
161164
}
162165

166+
167+
public static <T> ListenableFuture<T> invokeOnEDTFuture(Callable<T> callable) {
168+
ListenableFutureTask<T> future = ListenableFutureTask.create(callable);
169+
invokeOnEDT(future);
170+
return future;
171+
}
172+
173+
public static ListenableFuture<Void> invokeOnEDTFuture(Runnable runnable) {
174+
return invokeOnEDTFuture(() -> {
175+
runnable.run();
176+
return null;
177+
});
178+
}
179+
163180
public static void invokeOnEDTAndWait(final Runnable runnable) {
164181
if (SwingUtilities.isEventDispatchThread()) {
165182
runnable.run();

0 commit comments

Comments
 (0)