title: Java: Thread passing parameters to another thread
body: <p>I have two java classes called Reader and Worker. Reader reads strings from a text file and passes these strings to Workers which sort these strings into a tree. So I create a number of Worker-threads and 1 Reader-thread:</p>
  
  <pre><code>        try {      
  
          /* Create and run worker processes. */
          workers = new TrieWorker[numberOfWorkers];
          for(int i = 0; i &lt; numberOfWorkers; i++)
          {
              workers[i] = new TrieWorker(this);
              new Thread(workers[i]).run();
          }
  
          /* Create and run reader process. */
          reader  = new TrieReader(this, filename);                        
          new Thread(reader).run();
  
      } catch(Exception e) {
          e.printStackTrace();
      }
  </code></pre>
  
  <p>When my Reader reads a string from a text file inside run()-method, it passes the string to one of the workers by calling</p>
  
  <pre><code>workers[i].add(string);
  </code></pre>
  
  <p>Workers are basically doing nothing until this method has been called. So I'm wondering, does Reader continue reading from a text file immediately after it passes this parameter to one of the Workers? Or does it return from this method only after Worker is done with the string? In other words, when one Thread passes parameters to another one, do they both continue to do their own thing unless Thread passing parameters wants some kind of an answer back?</p>
  
  <p>Hope you guys know what I mean, really hard to explain.</p>
  
  <p><strong>EDIT:</strong> Thanks for the good answers! Here's code of Reader class:</p>
  
  <pre><code>private static int numberOfNextWorker = 0;
  
      public void run() 
  {
      System.out.println("Reader run.");
  
      try {
          System.out.println("Reading " + filename);
          read();
      } catch (Exception ex) {
          ex.printStackTrace();
      }
  }
  
  public void read() throws Exception
  {
      File file = new File(filename);
      Scanner reader = new Scanner(file);
  
      if(file.canRead() == false)
          throw new Exception("file " + filename + " cannot be read.");
  
      long totalLength = file.length();
  
      while(reader.hasNextLine())
      {
  
              String text = reader.nextLine();          
              numberOfLines++;
  
              /* Passes the work on to workers. */
              if(numberOfNextWorker == (numberOfWorkers - 1))
                   numberOfNextWorker = 0;
              else
                   numberOfNextWorker++;
  
              workers[numberOfNextWorker].add(text); 
          }
  }
  </code></pre>
  
  <p>And here Worker:</p>
  
  <pre><code>    public void run() 
  {
      System.out.println("Worker run.");
  }
  
  void add(String text) 
  {
      numberOfStrings++;
      char[] chars = text.toCharArray();
  
      int i = 0;
      Node node = new Node();
  
      while (chars.length &gt; i) {
          node.value = chars[i];
          node = node.children;
          i++;
      }
  }
  </code></pre>
  
  <p>It's not doing anything wise yet :) I think I understood what you said. I have read this book about concurrency a lot and done some exercises on paper but haven't tried to code anything that much.</p>