<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Supple software comments on And more code (no need to wait)</title>
    <link>http://supplesoftware.com/</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Supple software comments</description>
    <item>
      <title>"And more code (no need to wait)" by petrovg</title>
      <description>&lt;p&gt;Thanks to &lt;a href="http://yuri.baulsupp.com/"&gt;Yuri&lt;/a&gt; for pointing out that you don&amp;#8217;t need to wait until all the results are done to start processing them. Here, we&amp;#8217;ve only got integers, but in reality it is likely that the individual results will be big and meaningful and actually take some time to interpret or do something with. To start working on them as soon as each one is ready, we use CompletionService:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
public class ParallelCompletionCounter {
    public int countWordsAsSoonAsFileFinishes(List&lt;File&gt; files) {
        CompletionService&lt;Integer&gt; completionService = new ExecutorCompletionService&lt;Integer&gt;(Executors.newFixedThreadPool(4));
        for(final File file : files) {
            Callable&lt;Integer&gt; counter = new Callable&lt;Integer&gt;() {
                public Integer call() {
                    System.out.println("Starting file " + file.getName() + " at " + System.currentTimeMillis());
                    int time = 500;
                    if (file.getName().contains("304")) {
                        time = 5000;
                    }
                    try {
                        Thread.sleep(time);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    WordCounter counter = new WordCounter();
                    int count =  counter.count(file);
                    System.out.println("Done file " + file.getName() + " at " + System.currentTimeMillis());
                    return count;
                }
            };
            completionService.submit(counter);
        }
        try {
            int totalCount = 0;
            for (int i = 0; i&lt;4; i++) {
                totalCount += completionService.take().get();
                System.out.println("Added File Count");
            }
            return totalCount;
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        ParallelCompletionCounter counter = new ParallelCompletionCounter();
        List&lt;File&gt; files = new ArrayList&lt;File&gt;(args.length);
        for (String filename : args) {
            files.add(new File(filename));
        }
        System.out.println("Total word count is " + counter.countWordsAsSoonAsFileFinishes(files));
        System.out.println("Total elapsed time " + (System.currentTimeMillis() - start));
    }   
}
&lt;/code&gt;&lt;/pre&gt;

</description>
      <pubDate>Fri, 23 Nov 2007 01:53:18 PST</pubDate>
      <guid>&lt;a href="/articles/2007/11/23/and-more-code-no-need-to-wait"&gt;And more code (no need to wait)&lt;/a&gt;</guid>
      <link>&lt;a href="/articles/2007/11/23/and-more-code-no-need-to-wait"&gt;And more code (no need to wait)&lt;/a&gt;</link>
    </item>
  </channel>
</rss>
