简体   繁体   English

如何使联网的JavaFX应用程序正常退出?

[英]How can I get my networked JavaFX application to exit properly?

I am making a simple networked Tic-Tac-Toe game using JavaFX. 我正在使用JavaFX制作一个简单的网络井字游戏。 To initialize the server-client connection, I have two classes: serverSelectHandler and clientSelectHandler. 要初始化服务器-客户端连接,我有两个类:serverSelectHandler和clientSelectHandler。 To establish a connection, the server class creates a ServerSocket, creates a normal Socket, and then creates a Thread that handles data transmission. 为了建立连接,服务器类创建一个ServerSocket,创建一个普通的Socket,然后创建一个处理数据传输的线程。 The client class is the same, except that it does not have a ServerSocket. 客户端类是相同的,除了它没有ServerSocket。

When a Thread is created, it associates itself with the RemoteInputHandler class. 创建线程时,它将自己与RemoteInputHandler类相关联。 This class overrides the run() method in Runnable. 此类重写Runnable中的run()方法。 Here's a block of code that it adds: 这是它添加的代码块:

String input = netComm.reader.readLine();
while (input != null)
{
    // Make a separate copy of the input string 
    String inputCopy = input;
    // Post a work order to process the command on the GUI thread
    Platform.runLater( () -> {handleRemote(inputCopy);});
    // Get the next remote input
    input = netComm.reader.readLine();
}

As you can see, this method uses a lambda expression to queue up work orders. 如您所见,此方法使用lambda表达式将工作订单排队。 I have a feeling that this may be what is causing my problem. 我感觉这可能是导致我出现问题的原因。

The problem is that Platform.exit() does not fully terminate instances of this application. 问题在于Platform.exit()不会完全终止此应用程序的实例。 It causes the window to close, but the process stays running in the background. 这将导致窗口关闭,但进程仍在后台运行。 If I omit setting up a thread in ClientSelectHandler, this problem does not happen. 如果我省略在ClientSelectHandler中设置线程,则不会发生此问题。 However, this causes the client to ignore commands sent by the server. 但是,这导致客户端忽略服务器发送的命令。

http://jabahan.com/javafx-application-not-properly-closed-even-if-the-platform-exit-is-used/ http://jabahan.com/javafx-application-not-properly-closed-even-if-the-platform-exit-is-used/

The page linked above discusses a very similar problem. 上面链接的页面讨论了一个非常类似的问题。 This is why I think my problem may have something to do with extra threads that are not being closed. 这就是为什么我认为我的问题可能与未关闭的额外线程有关。 I can provide more code and/or explanation if necessary. 如果需要,我可以提供更多代码和/或解释。 Thanks for reading this. 感谢您阅读本文。

If you just want the application to exit immediately, you can call System.exit(). 如果只希望应用程序立即退出,则可以调用System.exit()。 However, if you want to be a little cleaner you'll have to override your Application.stop() method to close all your background threads before exiting. 但是,如果要更清洁一点,则必须在退出之前重写Application.stop()方法以关闭所有后台线程。 You might want to look at the Worker and Task classes to handle background threads. 您可能需要查看Worker和Task类来处理后台线程。

http://docs.oracle.com/javafx/2/threads/jfxpub-threads.htm http://docs.oracle.com/javafx/2/threads/jfxpub-threads.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM