开发者

Java: Creating a multi threaded reader

开发者 https://www.devze.com 2023-04-09 07:10 出处:网络
I\'m creating a reader application. The reader identifies based on the parameters which file to read, does some processing and returns the result to the caller.

I'm creating a reader application. The reader identifies based on the parameters which file to read, does some processing and returns the result to the caller.

I am trying to make开发者_运维问答 this multi-threaded, so that multiple requests can be processed. I thought it was simple but later realized it has some complexity. Even though i create threads using executor service, I still need to return the results back to the caller. So this means waiting for the thread to execute.

Only way i can think of is write to some common location or db and let the caller pick the result from there. Is there any approach possible?


Maybe an ExecutorCompletionService can help you. The submitted tasks are placed on a queue when completed. You can use the methods take or poll depending on if you want to wait or not for a task to be available on the completion queue.

ExecutorCompletionService javadoc


Use an ExecutorService with a thread pool of size > 1, post custom FutureTask derivatives which override the done() method to signal completion of the task to the UI:

public class MyTask extends FutureTask<MyModel> {
  private final MyUI ui;

  public MyTask(MyUI toUpdateWhenDone, Callable<MyModel> taskToRun) {
    super(taskToRun);
    ui=toUpdateWhenDone;
  }
  @Override
  protected void done() {
    try {
      // retrieve computed result
      final MyModel computed=get();
      // trigger an UI update with the new model
      java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
           ui.setModel(computed); // set the new UI model
        }
      });
    }
    catch(InterruptedException canceled) {
       // task was canceled ... handle this case here
    }
    catch(TimeoutException timeout) {
      // task timed out (if there are any such constraints). 
      // will not happen if there are no constraints on when the task must complete
    }
    catch(ExecutionException error) { 
      // handle exceptions thrown during computation of the MyModel object...
      // happens if the callable passed during construction of the task throws an 
      // exception when it's call() method is invoked.
    }
  }
}

EDIT: For more complex tasks which need to signal status updates, it may be a good idea to create custom SwingWorker derivatives in this manner and post those on the ExecutorService. (You should for the time being not attempt to run multiple SwingWorkers concurrently as the current SwingWorker implementation effectively does not permit it.)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号