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