3

I'm writing a Java GUI application that is doing some XML parsing and analysis. Since some of the methods take some time to complete, I made them into tasks (and thus was also able to utilize the netbeans autogenerated code to update the progress bar and message area of the main GUI). There are some areas of the code where I need to know the results of the first task before running the next task, but Java seems to run them in parallel by default.

Basically, if I ran the code below, I'd get it to print "finished task 1" while the task was running, and also it would also likely evaluate the next If statement as false (even if it were true) because the task had not yet finished.

I've been googling and trying few things and seem to have hit a wall. There were some posts about overriding the done() method of Task, which for some reason I cannot do because it's declared as a final. I can call task.get() on the main form/EDT, but that also blocks updating the GUI (making the progress bar irrelevant).

Some generalized code snippets:

From the main window (also the 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);    

        }

    }

From the interpreter / anotherInterpreter classes:

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;    

}
Shawn
  • 29
  • 1
  • 5

2 Answers2

1

you are probably searching for the CyclicBarrier which is part of the java concurrency package. It basically enables you to block one task until the other one clears the barrier.

See http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/CyclicBarrier.html for some details

light_303
  • 2,101
  • 2
  • 18
  • 35
  • This will block as requested, but you will have to spawn another thread so you don't block your EDT. – Thomas Dec 09 '11 at 18:58
  • Thanks, I think that's been part of my problem. I also think CountDownLatch will suffice and seems to make more sense. http://stackoverflow.com/questions/4168772/java-concurrency-countdown-latch-vs-cyclic-barrier – Shawn Dec 09 '11 at 19:10
0

I assume you are using tasks from the Swing app framework. You can use callbacks to execute some stuff you want after your current task completes executing in 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;    
}
shams
  • 3,460
  • 24
  • 24