Why is a thread blocking my JavaFX UI Thread?

后端 未结 1 1096
悲哀的现实
悲哀的现实 2020-12-11 17:35

I am trying to provide feedback in a JavaFX 8 application when a user chooses a menu item that launches a blocking process in another thread. In my real application it\'s a

相关标签:
1条回答
  • 2020-12-11 17:58

    The easiest way to do this is by using a Task. Platform.runLater is only needed if you need to update the UI from a different thread and therefore is not necessary in your case. If you would like to track progress of the background task while it is running, you may use updateMessage and updateProgress methods in the task to safely pass messages to the UI thread without worrying about the scheduling via EDT. You may find more information on this here https://docs.oracle.com/javase/8/javafx/interoperability-tutorial/concurrency.htm .

    See the minimal working example below.

    import javafx.application.Application;
    import javafx.concurrent.Task;
    import javafx.scene.Scene;
    import javafx.scene.control.MenuButton;
    import javafx.scene.control.MenuItem;
    import javafx.scene.control.ToolBar;
    import javafx.stage.Stage;
    
    
    public class BlockingThreadTestCase extends Application {
        public static void main(String[] args) {
            launch(args);
        }
    
        @Override
        public void start(Stage primaryStage) throws Exception {
            MenuItem menuItem = new MenuItem("Start");
            MenuButton menuButton = new MenuButton();
            menuButton.setText("Async Process");
            menuButton.getItems().addAll(menuItem);
    
            menuItem.setOnAction(event -> {
                menuButton.setText("Running...");
    
                Task task = new Task<Void>() {
                    @Override
                    public Void call() {
                        //SIMULATE A FILE DOWNLOAD
                        try {
                            Thread.sleep(5000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        return null;
                    }
                };
                task.setOnSucceeded(taskFinishEvent -> menuButton.setText(menuButton.getText() + "Done!"));
                new Thread(task).start();
            });
    
            final ToolBar toolbar = new ToolBar(menuButton);
            final Scene scene = new Scene(toolbar);
            primaryStage.setScene(scene);
            primaryStage.setWidth(150);
            primaryStage.show();
        }
    }
    
    0 讨论(0)
提交回复
热议问题