title: Blocking the output until input is finished using threads java
body: <p>I'm making a simple console chat application using sockets and threads. I have two different threads for sending and reading the messages. Everything works fine except one thing. I have to block the reader thread if I'm currently inside the sending thread. </p>
  
  <pre><code>@Override
  public void run() {
      input = In.readLine();
  
      while (!input.equals("x")) {
          if (input.isEmpty()) {
              Out.println("----------------------------");
              Out.print("Your message: ");
              message = In.readLine();
              Out.println("----------------------------");
  
              writer.println(message);
              writer.flush();
          }
  
          input = In.readLine();
      }
  }
  </code></pre>
  
  <p>Here is the reading thread</p>
  
  <pre><code>@Override
  public void run() {
      try {
          while ((receivedMessage = reader.readLine()) != null) {
              Out.println("Received message: " + receivedMessage);
          }
      } catch (IOException ex) {
  
      }
  }
  </code></pre>
  
  <p>What I have to do is following: If I'm currently writing a message "message = In.readLine()", and in the meantime I receive some messages, these should be displayed only after I finish the input.</p>