title: Multithreading - Counting total amount of words from several files
body: <p>I made a program to count words from individual files,
  but how can i modify my program, so it gives the total amount of words from all files (as ONE value).</p>
  
  <p>My code looks like this:</p>
  
  <pre><code>public class WordCount implements Runnable
  {
     public WordCount(String filename)
     {
        this.filename = filename;
     }
  
     public void run()
     {
        int count = 0;
        try
        {
           Scanner in = new Scanner(new File(filename));
  
           while (in.hasNext())
           {
              in.next();
              count++;
           }
           System.out.println(filename + ": " + count);
        }
        catch (FileNotFoundException e)
        {
           System.out.println(filename + " blev ikke fundet.");
        }
     }
     private String filename;
  }
  </code></pre>
  
  <p>With a Main-Class:</p>
  
  <pre><code>public class Main
  {
  
     public static void main(String args[])
     {
        for (String filename : args)
        {
           Runnable tester = new WordCount(filename);
  
           Thread t = new Thread(tester);
           t.start();
        }
     }
  }
  </code></pre>
  
  <p>And how to avoid race conditions?
  Thank you for your help.</p>