title: Java console output from another thread dropped
body: <p>I have a problem with the console output of one of my threads being dropped (in Java). The situation is as follows: I spawn a thread that listens (using a blocking method) for incoming messages. These messages are then asynchronously written to the console. Meanwhile, I read in the user input on the console via <code>System.console().readLine()</code>.</p>
  
  <p>The problem is that the text is never written to the console at all. I mean, as the <code>readLine()</code> method is blocking, I would have expected the console at least to show the output that has been written to the console as soon as something is entered in the main thread.. Or am I missing the point here?</p>
  
  <p>The relevant source is</p>
  
  <pre><code>// ...
  
  // handle receiving messages
  (new Thread() {
      @Override
      public void run() {
          while (executing) received(new String(subSocket.recv(0)));
      }
  }).start();
  
  // ...
  
  String input;
  try {
      while ((input = System.console().readLine()) != null &amp;&amp; !input.equals(".")) {
          pubSocket.send(input.getBytes(), 0);
          Thread.yield();
      }
  } 
  catch (Exception ex) { }
  finally {executing = false;}
  </code></pre>
  
  <p>And the received method is</p>
  
  <pre><code>public void received(String s) {
      System.console().format("(%s)", s);
      System.console().flush();
  }
  </code></pre>
  
  <p>What am I doing wrong? Or is there a better way to do this? I mean, I tried to use a <code>BufferedReader</code> encapsulating the input stream in order to read it linewise and used <code>System.out.format()</code> along with it.. To the same effect - Nothing :(.</p>
  
  <p>Cheers,
  fxx</p>