JavaFX中具有输入和输出参数的并发后台任务或服务

宁愿这是一个非常简单的概念,但是由于我对JavaFX的并发性完全陌生.我一直在努力理解这个概念.

在构建一个非常简单的JavaFX应用程序时,我想在后台执行一些冗长的任务,并使UI免受冻结.

在下面的示例中,我试图创建一个简单的后台任务服务,然后使用ControlsFX Dialog在主UI窗口上显示进度栏.

但是,我想知道如何为该服务提供一些输入参数并从中获取布尔输出.这在C#BackgroundWorker中确实很简单,但是在JavaFX中仍然无法弄清楚.任何提示将是一个很大的帮助.

尝试了这个简单的例子

    public void runTask(Stage stage) throws URISyntaxException, IOException {

    Service<Void> service = new Service<Void>() {

        @Override
        protected Task<Void> createTask() {
            return new Task<Void>() {
                @Override
                protected Void call() throws InterruptedException, URISyntaxException, IOException {
                    // some time consuming task here
                    // use the input arguments and perform some action on it
                    // then set the process result to a Boolean and return after the task is completed
                    // also keep hold back any other process to from executing on UI 
                    return null;
                }
            };
        }

    };

    Dialogs.create()
            .owner(stage)
            .title("Performing Task!")
            .masthead("Please wait...")
            .showWorkerProgress(service);

    service.setOnSucceeded(new EventHandler<WorkerStateEvent>() {

        @Override
        public void handle(WorkerStateEvent event) {
            System.out.println("done:" + event.getSource().getValue());
        }
    });

    service.setOnFailed(new EventHandler<WorkerStateEvent>() {

        @Override
        public void handle(WorkerStateEvent event) {
            throw new UnsupportedOperationException("Failed."); //To change body of generated methods, choose Tools | Templates.
        }
    });

    service.setOnCancelled(new EventHandler<WorkerStateEvent>() {

        @Override
        public void handle(WorkerStateEvent event) {
            throw new CancellationException("Cancelled."); //To change body of generated methods, choose Tools | Templates.
        }
    });

    service.start();

}

解决方法:

对于输入参数,只需为方法定义一些参数. (您需要它们是Java 7中的最终版本.)

要返回布尔值,只需将服务和任务的类型定义为布尔值:

public void runTask(Stage stage, final int input /* can be any type */) throws URISyntaxException, IOException {

    Service<Boolean> service = new Service<Boolean>() {

        @Override
        protected Task<Boolean> createTask() {
            return new Task<Boolean>() {
                @Override
                protected Boolean call() throws InterruptedException, URISyntaxException, IOException {
                    // some time consuming task here
                    // use the input arguments and perform some action on it
                    // then set the process result to a Boolean and return after the task is completed
                    // also keep hold back any other process to from executing on UI 

                    Boolean returnValue = ... ;
                    return returnValue;
                }
            };
        }

    };

    Dialogs.create()
            .owner(stage)
            .title("Performing Task!")
            .masthead("Please wait...")
            .showWorkerProgress(service);

    service.setOnSucceeded(new EventHandler<WorkerStateEvent>() {

        @Override
        public void handle(WorkerStateEvent event) {
            System.out.println("done:" + event.getSource().getValue());
        }
    });

    service.setOnFailed(new EventHandler<WorkerStateEvent>() {

        @Override
        public void handle(WorkerStateEvent event) {
            throw new UnsupportedOperationException("Failed."); //To change body of generated methods, choose Tools | Templates.
        }
    });

    service.setOnCancelled(new EventHandler<WorkerStateEvent>() {

        @Override
        public void handle(WorkerStateEvent event) {
            throw new CancellationException("Cancelled."); //To change body of generated methods, choose Tools | Templates.
        }
    });

    service.start();

}
上一篇:java-如何使用spring依赖注入来连接多个fxml控制器?


下一篇:如何通过JavaFX应用程序将变量传递给控制器​​?