<?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 More code</title>
    <link>http://www.supplesoftware.com/</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Supple software comments</description>
    <item>
      <title>"More code": comment by Yuri Schimke</title>
      <description>This is more suited to the CompletionService

http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/CompletionService.html

With the completion service, you don't have to worry about the order that the results execute in.  Your current example, it doesn't really matter.  But if the tasks can take variable amount of time, then you don't want to block on the first task, while you could be processing results of other finished tasks.</description>
      <pubDate>Thu, 22 Nov 2007 07:48:33 PST</pubDate>
      <guid>http://www.supplesoftware.com/articles/2007/11/22/more-code#comment-40</guid>
      <link>http://www.supplesoftware.com/articles/2007/11/22/more-code#comment-40</link>
    </item>
    <item>
      <title>"More code" by petrovg</title>
      <description>&lt;p&gt;I&amp;#8217;ve been exploring the java.util.concurrent package, and just thought I post some more code:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/p&gt;

&lt;pre&gt;
public class ParallelCounter {

    public int countWords1(List&lt;File&gt; files) {
        ExecutorService executorService = Executors.newFixedThreadPool(4);
        List&lt;Callable&lt;Integer&gt;&gt; counters = new ArrayList&lt;Callable&lt;Integer&gt;&gt;();
        for(final File file : files) {
            counters.add(new Callable&lt;Integer&gt;() {
                public Integer call() {
                    System.out.println("Starting file " + file.getName() + " at " + System.currentTimeMillis());
                    WordCounter counter = new WordCounter();
                    int count =  counter.count(file);
                    System.out.println("Done file " + file.getName() + " at " + System.currentTimeMillis());
                    return count;
                }
            });
        }
        try {
            List&lt;Future&lt;Integer&gt;&gt; results = executorService.invokeAll(counters);
            int totalCount = 0;
            for (Future&lt;Integer&gt; future : results) {
                totalCount += future.get();
            }
            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();
        ParallelCounter counter = new ParallelCounter();
        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.countWords1(files));
        System.out.println("Total elapsed time " +
(System.currentTimeMillis() - start));
    }
}
&lt;/pre&gt;

&lt;p&gt;&lt;/code&gt;&lt;/p&gt;

</description>
      <pubDate>Thu, 22 Nov 2007 05:06:12 PST</pubDate>
      <guid>&lt;a href="/articles/2007/11/22/more-code"&gt;More code&lt;/a&gt;</guid>
      <link>&lt;a href="/articles/2007/11/22/more-code"&gt;More code&lt;/a&gt;</link>
    </item>
  </channel>
</rss>
