【发布时间】:2014-05-04 15:08:07
【问题描述】:
我正在编写一个 JavaFX 应用程序,并且我的对象扩展了 Task 以提供远离 JavaFX GUI 线程的并发性。
我的 Main 类如下所示:
public class MainApp extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Sample.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
public void handle(WindowEvent t) {
//I have put this in to solve the threading problem for now.
Platform.exit();
System.exit(0);
}
});
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
我的 GUI 控制器示例如下所示(略微抽象):
ExecutorService threadPool = Executors.newFixedThreadPool(2);
private void handleStartButtonAction(ActionEvent event) {
MyTask task = new MyTask();
threadPool.execute(task);
}
目前我的任务只是休眠并打印数字 1 到 10:
public class MyTask extends Task<String> {
@Override
protected String call() throws Exception {
updateProgress(0.1, 10);
for (int i=0;i<=10;i++) {
if (isCancelled()) {
break;
}
Thread.sleep(1000);
System.out.println(i);
updateProgress(i, 10);
}
return "Complete";
}
}
我遇到的问题是,一旦任务完成,它看起来好像启动任务的线程继续运行。因此,当我通过按右上角的“X”退出 JavaFX 应用程序时,JVM 继续运行并且我的应用程序不会终止。如果你看看我的主课,我已经输入了 System.exit() 似乎可以解决问题,尽管我知道这不是正确的方法。
有人可以建议我在终止我的子线程方面需要做什么,以及这样做的可接受方式是什么?例如,检查它们是否完整然后终止。
谢谢
【问题讨论】:
标签: multithreading javafx task