title: Proper synchronization of Java threads using wait/notifyAll?
body: <p>Here is a simplified version of my application showing what I'm doing. </p>
  
  <pre><code>/*
  in my app's main():
  
      Runner run = new Runner();
  
      run.dowork();
  
  */
  
  class Runner
  {
      private int totalWorkers = 2;
      private int workersDone = 0;
  
      public synchronized void workerDone()
      {
          workersDone++;
          notifyAll();
      }
  
      public synchronized void dowork()
      {
          workersDone = 0;
  
          //&lt;code for opening a file here, other setup here, etc&gt;
  
          Worker a = new Worker(this);
          Worker b = new Worker(this);
  
          while ((line = reader.readLine()) != null)
          {
              //&lt;a large amount of processing on 'line'&gt;
  
              a.setData(line);
              b.setData(line);
  
              while (workersDone &lt; totalWorkers)
              {
                  wait();
              }               
          }
      }
  }
  
  class Worker implements Runnable
  {
      private Runner runner;
      private String data;
  
      public Worker(Runner r)
      {
          this.runner = r;
          Thread t = new Thread(this);
          t.start();
      }
  
      public synchronized void setData(String s)
      {
          this.data = s;
          notifyAll();
      }
  
      public void run
      {
          while (true)
          {
              synchronized(this)
              {
                  wait();
  
                  //&lt;do work with this.data here&gt;
  
                  this.runner.workerDone();
              }
          }
      }
  }
  </code></pre>
  
  <p>The basic concept here is that I have a bunch of workers which all do some processing on an incoming line of data, all independently, and write out the data wherever they like - they do not need to report any data back to the main thread or share data with each other. </p>
  
  <p>The problem that I'm having is that this code deadlocks. I'm reading a file of over 1 million lines and I'm lucky to get 100 lines into it before my app stops responding.</p>
  
  <p>The workers, in reality, all do differing amounts of work so I want to wait until they all complete before moving to the next line.</p>
  
  <p>I cannot let the workers process at different speeds and queue the data internally because the files I am processing are too large for this and won't fit in memory.</p>
  
  <p>I cannot give each worker its own FileReader to independently get 'line', because I do a ton of processing on the line before the workers see it, and do not want to have to re-do the processing in each worker.</p>
  
  <p>I know I'm missing some fairly simple aspect of synchronization in Java but I'm stuck at this point. If someone could explain what I'm doing wrong here I would appreciate it. I believe I'm misunderstanding some aspect of the synchronization but I'm out of ideas for attempting to fix it.</p>