【问题标题】:Java(FX) UI Update and (background) download taskJava(FX) UI 更新和(后台)下载任务
【发布时间】:2016-11-28 12:11:49
【问题描述】:

我编写了一个小型应用程序,其中包括对 JIRA REST API 的 REST 调用来解决下载问题。我有一个进度条指示下载进度。但是,实际上我需要进行两次 REST 调用。第一个需要在第二个开始之前完成。在他们之间,我需要更新 UI。

下面是一些示例代码:

// Loads the dialog from the fxml file and shows it
@FXML
public void showProgressDialog() {
    mainApp.showDownloadProgressDialog();
}

// Retrieves some meta information
public void firstDownload() {
    Task<Void> task = new Task<Void>() {
        @Override
        public Void call() {
            // do something
        }
    };
    new Thread(task).start();
}
// Do some UI update

// Retrieves the actual issues
public void secondDownload() {
    Task<Void> task = new Task<Void>() {
        @Override
        public Void call() {
            for (int i = 0; i < limit; i++) {
                // so something
                updateProgress((double) i / (double) limit, max);
            }
        }
    };
    new Thread(task).start();
}
// Do some UI update

我怎样才能确保上面显示的所有功能都按照这个顺序执行?

感谢您的帮助!

【问题讨论】:

  • 启动任务的2个方法是怎么调用的?为什么不能将它们组合成一个 Task
  • 谢谢,现在找到了一个只有一个任务的解决方案!

标签: java javafx concurrency synchronization


【解决方案1】:

您可以使用带有单个线程的EcecutorService 来控制调度:

ExecutorService service = Executors.newSingleThreadExecutor();

service.submit(task1);
service.submit(task2);

// shutdown after last submitted task
service.shutdown();

【讨论】:

  • 谢谢,我也试过了,它帮助我解决了类似的问题!
猜你喜欢
  • 1970-01-01
  • 2015-07-20
  • 2013-10-08
  • 1970-01-01
  • 1970-01-01
  • 2014-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多