gpt4 book ai didi

JavaFX 和套接字 = 不在 FX 应用程序线程上

转载 作者:行者123 更新时间:2023-11-30 07:48:34 25 4
gpt4 key购买 nike

我需要等待服务器程序使用套接字将数据发送到客户端程序,因此我必须使用 while 循环等待它。然而,客户端程序是一个 JavaFX 应用程序,如果在 while 循环中使用,它将卡住并崩溃,因此我将 while 循环放在一个新线程中。然而,这个 while 循环的主体需要更新 JavaFX UI,这是无法完成的,因为它会导致“Not on FX application thread;”异常,所以我无法为其创建新线程。

这是我的代码:

import static util.Constants.PORT;
import static util.Constants.SERVER_NAME;

public class Client extends Application {

private static View view;
public static Scanner in;
public static PrintWriter out;
private static boolean appRunning = true;

public static void main(String[] args) {
try {
Socket socket = new Socket(SERVER_NAME, PORT);
in = new Scanner(socket.getInputStream());
out = new PrintWriter(socket.getOutputStream(), true);

launch(args);
} catch (IOException e) {
System.out.println("Could not establish connection to server. Program terminating..");
System.exit(1);
}
}

@Override
public void start(Stage window) throws Exception {
// This is a JavaFX BorderPane that adds itself to window:
view = new View(window);

// ServerListener
new Thread(() -> {
try {
while (appRunning) {
// will through exception. needs to run on Application thread:
parseServerMessage(Client.in.nextLine());
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}).start();
}

private static String[] parseServerMessage(String message0 {
// update the JavaFX UI
}
}

如果我在没有线程的启动方法中使用下面的代码,JavaFX 应用程序将卡住:

@Override
public void start(Stage window) throws Exception {
// This is a JavaFX BorderPane that adds itself to window:
view = new View(window);

// causes JavaFX to freeze:
while (appRunning) {
parseServerMessage(Client.in.nextLine());
}
}

将线程置于 sleep 状态也没有帮助。我该如何解决这个问题?谢谢!

编辑解决方案:

感谢这个解决方案,我编辑了代码,现在它可以完美运行。这是编辑后的解决方案:

new Thread(() -> {
while (true) {
String serverMessage = Client.in.nextLine();
Platform.runLater(() -> {
parseServerMessage(serverMessage);
});
}
}).start();

最佳答案

您可以看看Platform::runLater 。来自 JavaDoc:

Run the specified Runnable on the JavaFX Application Thread at some unspecified time in the future. This method, which may be called from any thread, will post the Runnable to an event queue and then return immediately to the caller.

关于JavaFX 和套接字 = 不在 FX 应用程序线程上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33581330/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com