Every now and again I want to use System.out.prinln
to debug things instead of using a debugger, or I want a simple program to write to standard output so that I can log something without taking the time to get proper logging set up. I've noticed that sometimes my text winds up getting printed out of order.
E.g.:
System.out.println("A");
System.out.println("B");
System.out.println("C");
might result in
A
C
B
being printed.
I'm pretty sure I'm not crazy, so I have two questions:
- Why does this happen?
- What's an easy way I can keep this from happening?
EDIT: More information:
I'm running unit tests that build Lucene queries with JUnit. To print them out I've written this class:
public class LogHelper { //TODO-DAVID remove
public static final boolean ENABLED = true;
public static final boolean DISABLED = false;
private boolean enabled;
public LogHelper(boolean enabled){
this.enabled = enabled;
}
public synchronized void debug(String someString){
if(enabled){
System.out.println(someString);
}
}
}
I tried making 'debug()' synchronized, just in case multiple threads were calling it, but strange printing still occasionally happens out of order.