【问题标题】:Waiting for a task to finish in Java before starting another task在开始另一个任务之前等待一个任务在 Java 中完成
【发布时间】:2012-01-17 00:06:36
【问题描述】:

我正在编写一个 Java GUI 应用程序,它正在执行一些 XML 解析和分析。由于某些方法需要一些时间才能完成,因此我将它们变成了任务(因此也能够利用 netbeans 自动生成的代码来更新主 GUI 的进度条和消息区域)。在运行下一个任务之前,我需要在代码的某些区域知道第一个任务的结果,但 Java 似乎默认并行运行它们。

基本上,如果我运行下面的代码,我会让它在任务运行时打印“完成的任务 1”,而且它也可能将下一个 If 语句评估为假(即使它是真的)因为任务还没有完成。

我一直在谷歌上搜索并尝试了一些东西,但似乎碰壁了。有一些关于覆盖 Task 的 done() 方法的帖子,由于某种原因我不能这样做,因为它被声明为 final。我可以在主窗体/EDT 上调用 task.get(),但这也会阻止更新 GUI(使进度条无关紧要)。

一些通用代码sn-ps:

从主窗口(也是 EDT)

    @Action
    private void someAction(java.awt.event.ActionEvent evt) {

        ApplicationContext C = getApplication().getContext();
        TaskMonitor M = C.getTaskMonitor();
        TaskService S = C.getTaskService();

        Task task = interpreter.parse();

        S.execute(task);
        M.setForegroundTask(task);

        System.out.println("finished task 1");

        if (interpreter.someBool() == true) {

            task = anotherInterpreter.parse();
            S.execute(task);
            M.setForegroundTask(task);    

        }

    }

来自解释器/anotherInterpreter 类:

public Task parse() {


    Task task = new Task( org.jdesktop.application.Application.getInstance() ) {

        @Override
        protected Void doInBackground()  {

        // parse the file

        // set someBool to true if the another interpreter needs to be run

            return null;       

        }     

    };

    return task;    

}

【问题讨论】:

    标签: java background task synchronous swingworker


    【解决方案1】:

    您可能正在搜索作为 java 并发包的一部分的 CyclicBarrier。它基本上使您能够阻止一项任务,直到另一项任务清除障碍。

    详情请见http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/CyclicBarrier.html

    【讨论】:

    • 这将按要求阻止,但您必须生成另一个线程,以免阻止您的 EDT。
    • 谢谢,我认为这是我的问题的一部分。我也认为 CountDownLatch 就足够了,而且似乎更有意义。 stackoverflow.com/questions/4168772/…
    【解决方案2】:

    我假设您使用的是来自 Swing 应用程序框架的tasks。 当前任务在doInBackground() 中执行完成后,您可以使用回调来执行一些您想要的东西。

    public class CustomTask<T, V> extends Task<T, V> {
      private Runnable callback = null;
      // add appropriate constructors
      public void setCallback(final Runnable r) {
        callback = r;
      }
      protected void executeCallback() {
        if (callback != null) {
          callback.run();
        }
      }
    }
    
    
    @Action
    private void someAction(java.awt.event.ActionEvent evt) {
      final ApplicationContext C = getApplication().getContext();
      final TaskMonitor M = C.getTaskMonitor();
      final TaskService S = C.getTaskService();
    
      CustomTask task = interpreter.parse();
      task.setCallback(new Runnable(){
        public void run() {
          CustomTask t2 = anotherInterpreter.parse();
          S.execute(t2);
          M.setForegroundTask(t2);
        }
      });
      S.execute(task);
      M.setForegroundTask(task);
    
      System.out.println("finished task 1");
    }
    
    
    public CustomTask parse() {
      CustomTask task = new CustomTask( org.jdesktop.application.Application.getInstance() ) {
        @Override
        protected Void doInBackground()  {
    
          // parse the file
          executeCallback();
          return null;       
        }     
      };
      return task;    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-23
      • 1970-01-01
      • 2022-07-06
      相关资源
      最近更新 更多