JavaFX中的多线程挂起了UI [英] Multithreading in JavaFX hangs the UI

查看:577
本文介绍了JavaFX中的多线程挂起了UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的JavaFX 2应用程序,有2个按钮,分别是Start和Stop。单击开始按钮时,我想创建一个后台线程,它将进行一些处理并在其进行时更新UI(例如进度条)。如果单击停止按钮,我希望线程终止。

I have a simple JavaFX 2 app, with 2 buttons, saying Start and Stop. When the start button is clicked, I want to create a background thread which will do some processing and update the UI (e.g a progress bar) as it goes along. If the stop button is clicked, I want the thread to terminate.

我尝试使用 javafx.concurrent.Task 我从文档中收集的类可以正常工作。但是每当我点击开始时,用户界面会冻结/挂起而不是保持正常。

I've tried to do this using the javafx.concurrent.Task class which I gathered from the documentation would work fine for this. But whenever I click Start, the UI freezes/hangs rather than staying normal.

她是主要代码 Myprogram扩展应用程序用于显示按钮的类:

Her's the code from the main Myprogram extends Application class for showing the buttons:

public void start(Stage primaryStage)
{               
    final Button btn = new Button();
    btn.setText("Begin");

    //This is the thread, extending javafx.concurrent.Task :
    final MyProcessor handler = new MyProcessor();
    btn.setOnAction(new EventHandler<ActionEvent>()
    {
        public void handle(ActionEvent event)
        {                
           handler.run(); 
        }
    });

    Button stop = new Button();
    stop.setText("Stop");
    stop.setOnAction(new EventHandler<ActionEvent>()
        {
             public void handle(ActionEvent event)
             {
                handler.cancel();
             }
        }

    );
    // Code for adding the UI controls to the stage here.
}

这是 MyProcessor class:

import javafx.concurrent.Task;
public class MyProcessor extends Task
{   
    @Override
    protected Integer call()
    {
        int i = 0;
        for (String symbol : feed.getSymbols() )
        {
            if ( isCancelled() )
            {
                Logger.log("Stopping!");
                return i;
            }
            i++;
            Logger.log("Doing # " + i);
            //Processing code here which takes 2-3 seconds per iteration to execute
            Logger.log("# " + i + ", DONE! ");            
        }
        return i;
    }
}

非常简单,但每当我点击时,用户界面就会挂起开始按钮,虽然控制台消息继续显示( Logger.log 只是 System.out.println

Pretty simple, but the UI hangs whenever I click the Start button, though the console messages continue to get displayed (Logger.log simply does System.out.println )

我做错了什么?

推荐答案

任务实现 Runnable ,所以当你调用 handler.run(); 时你实际上是在UI线程中运行调用方法。这将挂起UI。

Task implements Runnable, so when you call handler.run(); you actually run the call method in the UI Thread. That will hang the UI.

您应该在后台线程中启动任务,通过执行程序或只需调用 new Thread(handler) .start();

You should start the task in a background thread, either via an executor or simply by calling new Thread(handler).start();.

这在 javadoc JavaFX并发教程

这篇关于JavaFX中的多线程挂起了UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆