title: How to keep sysout and syserr streams from intermixing?
body: <p>In my code base is the (very simplified) following:</p>
  
  <pre><code>public static void main (String[] args) {
     System.out.println("Starting application");
     try {
        System.out.println("About to validate");
        validate(args);
     catch (Exception e) {
        e.printStackTrace();
     }
  }
  
  public static void validate(String[] args) {
     System.out.println("Your first arg is " + args[0]);
     if (someProblemWith(args)) {
        System.out.println("Your args are wrong.  It should be: ...");
        throw new BadArgsException(e);
     }
  }
  </code></pre>
  
  <p>Which works fine.  Note that my example code above is contrived and simply meant to show multiple log statements prior to exception and stack trace printing.  This often means that my last logging statement is lost in the middle of the stack trace output.  Is there an elegant way to ask the <code>e.printStackTrace()</code> statement to wait until the System.out has finished its work?  I'm essentially looking for the stacktrace to be the very last thing printed when an error occurs.  Here's a sample output of my program above:</p>
  
  <pre><code> java.lang.Throwable
   ....
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   Your args are wrong.  It should be: ...
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:56)
  </code></pre>