title: Java: synchronizing standard out and standard error
body: <p>I have a strange problem and it would be nice if I could solve it. For the debugging purposes (and some other things, as well) I'm writing a log of a console Java application on the standard output. Some things that are writen on standard out, and some things like errors are printed on standard error. The problem is that these two are not perfectly synchronized, so the order of printed lines is not always correct. I guess this is because many things are printed and it happens that a buffer for one output is full so the other output prints before the first one flushes it buffer.</p>
  
  <p>E.g., I want to write this:</p>
  
  <pre><code>syso: aaa
  syso: bbb
  syso: ccc
  syso: ddd
  syso: eee
  syserr: ---
  </code></pre>
  
  <p>What is sometimes printed is</p>
  
  <pre><code>aaa
  bbb
  ccc
  ---
  ddd
  eee
  </code></pre>
  
  <p>Sometimes there is not a new line in between, so it looks like</p>
  
  <pre><code>aaa
  bbb
  ccc---
  
  ddd
  eee
  </code></pre>
  
  <p>Every time I print something on an output, I flush the same output with</p>
  
  <pre><code>System.out.flush();
  </code></pre>
  
  <p>or</p>
  
  <pre><code>System.err.flush();
  </code></pre>
  
  <p>How to solve this problem?
  Btw, everything is printed in the Eclipse console.</p>