在JAVAFX中关闭窗口时停止客户端线程

时间:2016-03-04 06:24:54

标签: javafx javafx-2 javafx-8

我想在客户端窗口关闭时停止客户端线程。以下是我的代码:

Client.java

public class Client extends Task {

private int portNumber = 8080;
private String hostName = "127.0.0.1";

public Client(){
    stage = new Stage();
    StackPane root = new StackPane();

    stage.setScene(new Scene(root, 200, 250));
    stage.show();
}


@Override
protected Object call() throws Exception {
    try (Socket socket = new Socket(hostName, portNumber);
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));) {
        }
    } catch (UnknownHostException e) {
    } catch (IOException e) {
    }
    return null;
}
}

通过致电客户:

new Thread(new Client()).start();

这在serverController

内调用

当客户端窗口关闭时,如何停止/终止线程?谢谢!

编辑:

通过以下示例管理以使其工作:

http://docs.oracle.com/javase/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html

0 个答案:

没有答案