0

I'm running into an issue with JavaFX multi threading if anyone can help. Please see the code below. Thanks!

import ...

public class Session extends Application implements Runnable {
    String srcIP;

    public Session() {
    }

    @Override
    public void run() {
        launch();
        System.out.println("New thread running with IP: " + srcIP);
    }

    public static void main(String args[]) {
        new Thread(new Session()).start();
        new Thread(new Session()).start();
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Hello World!");
        StackPane root = new StackPane();
        root.setStyle("-fx-background-color: BLACK;");
        primaryStage.setScene(new Scene(root, 400, 330));
        primaryStage.show();
    }
}

Here is the console output. It will run the first thread just fine. But the second thread gets caught up in the following error.

   Exception in thread "Thread-1" java.lang.IllegalStateException: Application launch must not be called more than once
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:94)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:75)
    at javafx.application.Application.launch(Application.java:209)
    at guiFX.Session.run(Session.java:27)
    at java.lang.Thread.run(Thread.java:744)
New thread running with IP: null
user2799603
  • 873
  • 3
  • 13
  • 19

1 Answers1

0

From JavaDoc:

public static void launch(java.lang.String... args)

Launch a standalone application. This method is typically called from the main method(). It must not be called more than once or an exception will be thrown.

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417