我是javafx上的新手,我想在我的项目上实现并发,这允许我编写多线程应用程序的代码。为此,我做了以下事情:
Task<Integer> task = new Task<Integer>() {
@Override protected Integer call() throws Exception {
/// long task...
sitioFind(); // downloads text from a website (HttpStatusException is always thrown and that's when the long task freezes)
newEpi(); //downloads an image from a website (the program never reaches this point of the code because it freezes before).
//Notice that the UI doesn't freeze. The UI thread seems to be working fine because it never freezes. The problem is with the running task: exception thrown, program stops working.
return null;
}
};
new Thread(task).start();
启用多线程的代码工作正常(执行长任务并同时更新UI)。但是,当且仅当长任务不会抛出任何异常时,这才有效。
如果抛出任何异常,应用程序只会冻结并停止执行长任务,因此UI不会更新。
所以,我做的是研究javaFX文档,我发现了一些文字:
成功完成的Worker对象的状态为SUCCEEDED,value属性设置为此Worker对象的结果。 否则,如果在执行Worker对象期间抛出任何异常,则其状态变为FAILED,并且异常属性设置为发生的异常类型
因此,根据前一段,该州已失败,该计划因此没有继续下去。代码将始终抛出异常(需要抛出异常并且严格需要这样)但状态变为FAILED并且程序不会继续。
我需要的是一种避免程序状态变为FAILED的方法,或者是一种在程序状态发生变化时阻止UI冻结的方法。谢谢。