0

如果有人可以提供帮助,我会遇到 JavaFX 多线程问题。请看下面的代码。谢谢!

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

这是控制台输出。它会运行第一个线程就好了。但是第二个线程陷入了以下错误。

   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
4

1 回答 1

0

来自JavaDoc

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

启动一个独立的应用程序。此方法通常从 main 方法 () 调用。它不能被多次调用,否则会抛出异常。

于 2013-10-29T03:39:28.983 回答