From d466776f0498ce543b74e003b19ef6f27428b681 Mon Sep 17 00:00:00 2001 From: Sesu8642 <45859254+Sesu8642@users.noreply.github.com> Date: Mon, 9 Sep 2024 22:18:31 +0200 Subject: [PATCH] refactor executors to use daemon threads --- .../sesu8642/feudaltactics/FeudalTactics.java | 3 --- .../dagger/FeudalTacticsComponent.java | 7 ------ .../dagger/MainDaggerModule.java | 23 ++++--------------- .../ingame/dagger/IngameDaggerModule.java | 3 ++- .../crashreporting/ui/CrashReportScreen.java | 4 +--- 5 files changed, 8 insertions(+), 32 deletions(-) diff --git a/core/src/main/java/de/sesu8642/feudaltactics/FeudalTactics.java b/core/src/main/java/de/sesu8642/feudaltactics/FeudalTactics.java index 3e43457a..b3cfbf61 100644 --- a/core/src/main/java/de/sesu8642/feudaltactics/FeudalTactics.java +++ b/core/src/main/java/de/sesu8642/feudaltactics/FeudalTactics.java @@ -30,9 +30,6 @@ public void create() { @Override public void dispose() { - // shutdown executor services to kill all background threads - component.getBotAiExecutor().shutdownNow(); - component.getCopyButtonExecutor().shutdownNow(); super.dispose(); } diff --git a/core/src/main/java/de/sesu8642/feudaltactics/dagger/FeudalTacticsComponent.java b/core/src/main/java/de/sesu8642/feudaltactics/dagger/FeudalTacticsComponent.java index ec0f2f68..61fdd864 100644 --- a/core/src/main/java/de/sesu8642/feudaltactics/dagger/FeudalTacticsComponent.java +++ b/core/src/main/java/de/sesu8642/feudaltactics/dagger/FeudalTacticsComponent.java @@ -2,9 +2,6 @@ package de.sesu8642.feudaltactics.dagger; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.ScheduledExecutorService; - import javax.inject.Singleton; import dagger.Component; @@ -31,10 +28,6 @@ public interface FeudalTacticsComponent { GameInitializer getGameInitializer(); - ExecutorService getBotAiExecutor(); - - ScheduledExecutorService getCopyButtonExecutor(); - GameCrasher getGameCrasher(); } diff --git a/core/src/main/java/de/sesu8642/feudaltactics/dagger/MainDaggerModule.java b/core/src/main/java/de/sesu8642/feudaltactics/dagger/MainDaggerModule.java index 97227c28..82278920 100644 --- a/core/src/main/java/de/sesu8642/feudaltactics/dagger/MainDaggerModule.java +++ b/core/src/main/java/de/sesu8642/feudaltactics/dagger/MainDaggerModule.java @@ -2,9 +2,6 @@ package de.sesu8642.feudaltactics.dagger; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; - import javax.inject.Singleton; import org.slf4j.Logger; @@ -14,7 +11,6 @@ import com.badlogic.gdx.InputMultiplexer; import com.badlogic.gdx.scenes.scene2d.ui.Skin; import com.google.common.eventbus.EventBus; -import com.google.common.eventbus.SubscriberExceptionContext; import com.google.common.eventbus.SubscriberExceptionHandler; import com.ray3k.stripe.FreeTypeSkin; @@ -33,14 +29,11 @@ private MainDaggerModule() { @Provides @Singleton static EventBus provideEventBus() { - return new EventBus(new SubscriberExceptionHandler() { - @Override - public void handleException(Throwable exception, SubscriberExceptionContext context) { - Logger logger = LoggerFactory.getLogger(SubscriberExceptionHandler.class.getName()); - logger.error(String.format( - "an unexpected error happened while handling the event %s in method %s of subscriber %s", - context.getEvent(), context.getSubscriberMethod(), context.getSubscriber()), exception); - } + return new EventBus((exception, context) -> { + Logger logger = LoggerFactory.getLogger(SubscriberExceptionHandler.class.getName()); + logger.error(String.format( + "an unexpected error happened while handling the event %s in method %s of subscriber %s", + context.getEvent(), context.getSubscriberMethod(), context.getSubscriber()), exception); }); } @@ -55,10 +48,4 @@ static InputMultiplexer provideInputMultiplexer() { return new InputMultiplexer(); } - @Provides - @Singleton - static ScheduledExecutorService provideCopyButtonFeedbackExecutorService() { - return Executors.newSingleThreadScheduledExecutor(); - } - } \ No newline at end of file diff --git a/core/src/main/java/de/sesu8642/feudaltactics/ingame/dagger/IngameDaggerModule.java b/core/src/main/java/de/sesu8642/feudaltactics/ingame/dagger/IngameDaggerModule.java index 66a4d2bc..d1de0102 100644 --- a/core/src/main/java/de/sesu8642/feudaltactics/ingame/dagger/IngameDaggerModule.java +++ b/core/src/main/java/de/sesu8642/feudaltactics/ingame/dagger/IngameDaggerModule.java @@ -92,7 +92,8 @@ static Preferences provideIncrementalAutoSavePrefStore(@PreferencesPrefixPropert @Provides @Singleton static ExecutorService provideBotAiExecutor() { - return Executors.newSingleThreadExecutor(new ThreadFactoryBuilder().setNameFormat("botai-%d").build()); + return Executors + .newSingleThreadExecutor(new ThreadFactoryBuilder().setNameFormat("botai-%d").setDaemon(true).build()); } } diff --git a/core/src/main/java/de/sesu8642/feudaltactics/menu/crashreporting/ui/CrashReportScreen.java b/core/src/main/java/de/sesu8642/feudaltactics/menu/crashreporting/ui/CrashReportScreen.java index 1db8633d..526ab675 100644 --- a/core/src/main/java/de/sesu8642/feudaltactics/menu/crashreporting/ui/CrashReportScreen.java +++ b/core/src/main/java/de/sesu8642/feudaltactics/menu/crashreporting/ui/CrashReportScreen.java @@ -4,7 +4,6 @@ import java.net.URI; import java.net.URISyntaxException; -import java.util.concurrent.ScheduledExecutorService; import javax.inject.Inject; import javax.inject.Singleton; @@ -46,8 +45,7 @@ public class CrashReportScreen extends GameScreen { /** Constructor. */ @Inject public CrashReportScreen(EventBus eventBus, @MenuCamera OrthographicCamera camera, @MenuViewport Viewport viewport, - CrashReportStage crashReportStage, CrashReportDao crashReportDao, - ScheduledExecutorService copyButtonFeedbackExecutorService) { + CrashReportStage crashReportStage, CrashReportDao crashReportDao) { super(camera, viewport, crashReportStage); this.eventBus = eventBus; this.crashReportStage = crashReportStage;