Multithreading – JavaFX uses threads and GUI

There was a problem using JavaFX and threads Basically, I have two choices: use tasks or platform runLater. As far as I know, platform Runlater should be used for simple / short tasks, while task is used for longer tasks But I can't use any of them

When I call thread, it must pop up a verification code dialog box in the middle of the task When using task, it ignores my request to display a new dialog... It does not allow me to create a new phase

On the other hand, when I use platform When runlater, it lets me display a dialog box, but the main window of the program freezes until the pop-up dialog box is displayed

I need any solution If someone knows how to deal with this or has some similar experience and finds a solution, I look forward to your reply!

Solution

As put it, you must use tasks or services to do what you need to do in the background And platform Runlater executes things in the JavaFX application thread from the background thread

You must synchronize them, one way of which is to use the countdownlatch class

Here is an example:

Service<Void> service = new Service<Void>() {
        @Override
        protected Task<Void> createTask() {
            return new Task<Void>() {           
                @Override
                protected Void call() throws Exception {
                    //Background work                       
                    final CountDownLatch latch = new CountDownLatch(1);
                    Platform.runLater(new Runnable() {                          
                        @Override
                        public void run() {
                            try{
                                //FX Stuff done here
                            }finally{
                                latch.countDown();
                            }
                        }
                    });
                    latch.await();                      
                    //Keep with the background work
                    return null;
                }
            };
        }
    };
    service.start();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>