Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make UserThread::run* methods thread safe #4122

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions core/src/main/java/bisq/core/offer/OpenOfferManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,8 @@ public OpenOfferManager(CreateOfferService createOfferService,
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
Expand Down Expand Up @@ -220,7 +219,9 @@ public void shutDown(@Nullable Runnable completeHandler) {
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 {
Expand Down
47 changes: 32 additions & 15 deletions desktop/src/main/java/bisq/desktop/common/UITimer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,31 +37,45 @@ public UITimer() {

@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);
}
}
}