Merge pull request #4122 from stejbac/make-UserThread-runAfter-thread-safe

Make UserThread::run* methods thread safe
This commit is contained in:
Christoph Atteneder 2020-04-10 15:55:03 +02:00 committed by GitHub
commit 34734c6fe4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 19 deletions

View file

@ -160,9 +160,8 @@ public class OpenOfferManager implements PeerManager.Listener, DecryptedDirectMe
openOfferTradableListStorage = storage;
// In case the app did get killed the shutDown from the modules is not called, so we use a shutdown hook
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
UserThread.execute(OpenOfferManager.this::shutDown);
}, "OpenOfferManager.ShutDownHook"));
Runtime.getRuntime().addShutdownHook(new Thread(() ->
UserThread.execute(OpenOfferManager.this::shutDown), "OpenOfferManager.ShutDownHook"));
}
@Override
@ -221,7 +220,9 @@ public class OpenOfferManager implements PeerManager.Listener, DecryptedDirectMe
int size = openOffers != null ? openOffers.size() : 0;
log.info("Remove open offers at shutDown. Number of open offers: {}", size);
if (offerBookService.isBootstrapped() && size > 0) {
openOffers.forEach(openOffer -> offerBookService.removeOfferAtShutDown(openOffer.getOffer().getOfferPayload()));
UserThread.execute(() -> openOffers.forEach(
openOffer -> offerBookService.removeOfferAtShutDown(openOffer.getOffer().getOfferPayload())
));
if (completeHandler != null)
UserThread.runAfter(completeHandler, size * 200 + 500, TimeUnit.MILLISECONDS);
} else {

View file

@ -18,8 +18,11 @@
package bisq.desktop.common;
import bisq.common.Timer;
import bisq.common.UserThread;
import bisq.common.reactfx.FxTimer;
import javafx.application.Platform;
import java.time.Duration;
import org.slf4j.Logger;
@ -34,31 +37,45 @@ public class UITimer implements Timer {
@Override
public Timer runLater(Duration delay, Runnable runnable) {
if (timer == null) {
timer = FxTimer.create(delay, runnable);
timer.restart();
} else {
log.warn("runLater called on an already running timer.");
}
executeDirectlyIfPossible(() -> {
if (timer == null) {
timer = FxTimer.create(delay, runnable);
timer.restart();
} else {
log.warn("runLater called on an already running timer.");
}
});
return this;
}
@Override
public Timer runPeriodically(Duration interval, Runnable runnable) {
if (timer == null) {
timer = FxTimer.createPeriodic(interval, runnable);
timer.restart();
} else {
log.warn("runPeriodically called on an already running timer.");
}
executeDirectlyIfPossible(() -> {
if (timer == null) {
timer = FxTimer.createPeriodic(interval, runnable);
timer.restart();
} else {
log.warn("runPeriodically called on an already running timer.");
}
});
return this;
}
@Override
public void stop() {
if (timer != null) {
timer.stop();
timer = null;
executeDirectlyIfPossible(() -> {
if (timer != null) {
timer.stop();
timer = null;
}
});
}
private void executeDirectlyIfPossible(Runnable runnable) {
if (Platform.isFxApplicationThread()) {
runnable.run();
} else {
UserThread.execute(runnable);
}
}
}