initSystems now uses latch instead of timer. Moved single threads to Executor

This commit is contained in:
lieght
2025-12-07 18:59:19 +01:00
parent 38f50cc16d
commit 80d3e47ad9

View File

@@ -33,6 +33,9 @@ import javafx.scene.layout.StackPane;
import javafx.stage.Stage; import javafx.stage.Stage;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public final class App extends Application { public final class App extends Application {
private static Stage stage; private static Stage stage;
@@ -46,7 +49,7 @@ public final class App extends Application {
} }
@Override @Override
public void start(Stage stage) throws Exception { public void start(Stage stage) {
// Start loading localization // Start loading localization
ResourceManager.loadAssets(new ResourceLoader("app/src/main/resources/localization")); ResourceManager.loadAssets(new ResourceLoader("app/src/main/resources/localization"));
ResourceManager.loadAssets(new ResourceLoader("app/src/main/resources/style")); ResourceManager.loadAssets(new ResourceLoader("app/src/main/resources/style"));
@@ -180,12 +183,20 @@ public final class App extends Application {
} }
private void initSystems() { // TODO Move to better place private void initSystems() { // TODO Move to better place
new Thread(() -> new NetworkingClientEventListener(
GlobalEventBus.get(),
new NetworkingClientManager(GlobalEventBus.get()))
).start();
new Thread(() -> { final int THREAD_COUNT = 2;
CountDownLatch latch = new CountDownLatch(THREAD_COUNT);
ExecutorService threads = Executors.newFixedThreadPool(THREAD_COUNT);
threads.submit(() -> {
new NetworkingClientEventListener(
GlobalEventBus.get(),
new NetworkingClientManager(GlobalEventBus.get()));
latch.countDown();
});
threads.submit(() -> {
MusicManager<MusicAsset> musicManager = MusicManager<MusicAsset> musicManager =
new MusicManager<>( new MusicManager<>(
GlobalEventBus.get(), GlobalEventBus.get(),
@@ -209,11 +220,11 @@ public final class App extends Application {
audioVolumeManager audioVolumeManager
).initListeners("medium-button-click.wav"); ).initListeners("medium-button-click.wav");
}).start(); latch.countDown();
});
// Threads must be ready, before continue, TODO use latch instead
try { try {
Thread.sleep(200); latch.await();
} catch (InterruptedException e) { } catch (InterruptedException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }