title: How to return messages from child threads to main thread(method) in Java?
body: <p>I have a HashMap of people who want to communicate with each other as follows:</p>
  
  <pre><code>{zidane=[rooney, rooney, rooney, scholes, rooney], rooney=[scholes, messi, scholes], scholes=[ronaldo], ronaldo=[rooney, messi, scholes], messi=[zidane]}
  </code></pre>
  
  <p>Here, each person in the key will have their own threads in which they will send a message to each person in their list and receive a response.</p>
  
  <pre><code>import java.util.*;
  
  public class Master {
      public Map callsMap = new HashMap&lt;String, List&gt;();
  
      public static void main(String[] args){
          Master m = new Master();
          m.readFile();
          Iterator&lt;Map.Entry&lt;String, List&gt;&gt; it = m.callsMap.entrySet().iterator();
          while (it.hasNext()) {
              Map.Entry&lt;String, List&gt; pair = it.next();
              String caller = pair.getKey();
              List receiverList = pair.getValue();
              SubTasks st = new SubTasks(caller, receiverList);
              Thread thread = new Thread(st);
              thread.start();
              //st.getMessage();
          }
      }
  }
  </code></pre>
  
  <p>The <code>master</code> class will iteratively create a thread for every key in the HashMap. Please note that the <code>readFile</code> method will just read from a text file and produce the input (into <code>callsMap</code> variable) I have mentioned above. It is not particularly important for this context so I have omitted it here.</p>
  
  <p>I have the <code>SubTasks</code> class which extends the <code>Thread</code> class and creates messages for each interaction. This class will not create any additional threads but just operate on the caller and receiver list it received as follows:</p>
  
  <pre><code>import java.util.List;
  
  public class SubTasks extends Thread {
      private String caller;
      private List receiverList;
      private volatile String returnMessage;
  
      SubTasks(String s, List l){
          caller = s;
          receiverList = l;
      }
  
      public void setMessage(String msg){
          returnMessage = msg;
      }
  
      public String getMessage() {
          return returnMessage;
      }
  
      @Override
      public void run(){
          for (int i = 0; i &lt; receiverList.size(); i++) {
              System.out.println(receiverList.get(i)+" received intro message from "+caller +" ["+System.currentTimeMillis()+"]");
              returnMessage = caller+" received reply message from "+receiverList.get(i) +" ["+System.currentTimeMillis()+"]";
              //setMessage(returnMessage);
              System.out.println(returnMessage);
          }
      }
  }
  </code></pre>
  
  <p>I get the desired output if I print the messages in the child threads for all 26 pairs.</p>
  
  <pre><code>zidane received message from messi [1592117172946]
  rooney received message from ronaldo [1592117172946]
  scholes received message from rooney [1592117172946]
  rooney received message from zidane [1592117172946]
  ronaldo received message from scholes [1592117172946]
  messi received reply from zidane [1592117172989]
  ronaldo received reply from rooney [1592117172989]
  ...
  ...
  </code></pre>
  
  <p>How do I pass these messages into the main thread (method) and print it there instead of the child threads? I tried using <code>getter-setter</code> methods but since the threads will execute randomly, there is no way to call the <code>getter</code> methods properly.</p>