controlling the order of submitted futures

Posted by mac on Stack Overflow See other posts from Stack Overflow or by mac
Published on 2010-06-10T18:39:13Z Indexed on 2010/06/10 18:42 UTC
Read the original article Hit count: 212

Filed under:
|

In this example, i am submitting a few files to my comparator object. It all works fine, except that i noticed that order in which files are submitted is not always teh same order in which they are returned. Any suggestions on how i can better control this?

 ExecutorService pool = Executors.newFixedThreadPool(5);
  CompletionService<Properties> completion = new ExecutorCompletionService<Properties>(pool);

  for (String target : p.getTargetFiles()) {
   completion.submit(new PropertiesLoader(target, p));
  }

  for (@SuppressWarnings("unused")
  String target : p.getTargetFiles()) {
   Properties r = null;
   try {
    r = completion.take().get();
   } catch (InterruptedException e) {
    e.printStackTrace();
   } catch (ExecutionException e) {
    e.printStackTrace();
   }

   p.addTargetFilesProperties(r);
  }

  pool.shutdown();

© Stack Overflow or respective owner

Related posts about java

Related posts about futures