System.out
returns the "standard" output stream - a PrintStream
. The javadoc of PrintStream
tells me nothing about thread safety but looking at the source of the OpenJDK and the OracleJDK tells me that println
is synchronized.
/**
* Prints a String and then terminate the line. This method behaves as
* though it invokes <code>{@link #print(String)}</code> and then
* <code>{@link #println()}</code>.
*
* @param x The <code>String</code> to be printed.
*/
public void println(String x) {
synchronized (this) {
print(x);
newLine();
}
}
That fits pretty well to my experiences: Calling System.out.println()
never created 'mixed' output when calling from different threads.
So my question(s):
- Can I rely on this behavior (using different JVMs)?
- Is there some documentation that I missed which describes this behavior?