There is a form and there is a static method that implements the launch of a multi-threaded server. The problem is this, if you register a call to this method in the controller, the form hangs as this method starts to work. How can I update the server and work in parallel? I understand it is necessary to split the program execution into two streams, one responsible for the interface, the other for server work, but how is it?

Main.java:

import Server.AUTO.Goods; import Server.NET.Server; import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.ActionEvent; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.cell.PropertyValueFactory; import javafx.scene.image.Image; import javafx.stage.Stage; import java.io.IOException; import java.net.URL; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.ResourceBundle; import static Server.DATABASE.Database.ExecuteStatement; import static Server.DATABASE.Database.GetInformationDatabase; import static Server.DATABASE.Database.getInstanceDatabase; import static Server.NET.Server.flag; import static Server.NET.Server.getInstanceServer; public class Main extends Application { public Button onServer; public Button offServer; public static Stage primaryStage; public static void setStage(Stage stage) { primaryStage = stage; } public static Stage getStage() {return primaryStage;} public static void setScene(Scene scene) { primaryStage.setScene(scene); } public static Scene getScene() { return primaryStage.getScene(); } public static void runStage(Stage stage) throws IOException { primaryStage = stage; primaryStage.setTitle("ServerProgram"); primaryStage.setMinHeight(300); primaryStage.setMinWidth(530); primaryStage.setMaxHeight(400); primaryStage.setMaxWidth(630); primaryStage.show(); } @Override public void start(Stage primaryStage) throws Exception{ Parent root = FXMLLoader.load(getClass().getResource("/Server/FXML/Scene1.fxml")); primaryStage.setScene(new Scene(root)); runStage(primaryStage); } public void startServer(ActionEvent actionEvent) throws IOException { onServer.setDisable(true); flag = true; offServer.setDisable(false); //Запуск сервера getInstanceServer(); //ЗАВИСАНИЕ ФОРМЫ } public static void main(String[] args) { launch(args); } } 

1 answer 1

Try to wrap the server start in Task .

Thus, the start method of the server will look something like this

 public void startServer(ActionEvent actionEvent) throws IOException { onServer.setDisable(true); flag = true; offServer.setDisable(false); //Запуск сервера Task < Void > task = new Task < Void > () { @Override protected Void call() throws Exception { //тут запускаем, например, прогресс бар getInstanceServer(); return null; } }; new Thread(task).start(); }