Application hang up after java serversocket listens

101 views Asked by At

I have a JavaFX application and invoke the following code with a Button click.

new Thread(() -> {
    ServerSocket serverSocket = null;
    try {
        serverSocket = new ServerSocket(4558);
        Socket accept = serverSocket.accept();
    } catch (IOException e) {
        e.printStackTrace();
    }
}).run();

With the thread, I want to prevent that the UI will be frozen, but the application has not returned any feedback, after serverSocket.accept(). The port is not blocked.

Does anyone know why? Thanks in advance!

1

There are 1 answers

0
user000001 On BEST ANSWER

When starting a Thread, don't call the run() method but the start() one.

Code would be like this:

new Thread(() -> {
    ServerSocket serverSocket = null;
    try {
        serverSocket = new ServerSocket(4558);
        Socket accept = serverSocket.accept();
    } catch (IOException e) {
        e.printStackTrace();
    }
}).start(); // was run();