gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:45:29 25 4
gpt4 key购买 nike

它宁愿是一个非常简单的概念,但因为我对 JavaFX 中的并发完全陌生。我一直在努力理解这个概念。

在构建一个非常简单的 JavaFX 应用程序时,我想在后台执行一些冗长的任务并防止 UI 卡住。

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

但是,我想知道如何为该服务提供一些输入参数并从中获取 boolean 值输出。这在 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 中是 final。)

要返回Boolean,只需将ServiceTask 的类型定义为Boolean:

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 - JavaFX 中具有输入和输出参数的并发后台任务或服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28049112/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com