Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is multi-thread output from System.out.println interleaved

If multiple threads call System.out.println(String) without synchronization, can the output get interleaved? Or is the write of each line atomic? The API makes no mention of synchronization, so this seems possible, or is interleaved output prevented by buffering and/or the VM memory model, etc.?

EDIT:

For example, if each thread contains:

System.out.println("ABC"); 

is the output guaranteed to be:

ABC ABC 

or could it be:

AABC BC 
like image 564
Ellen Spertus Avatar asked Feb 27 '12 03:02

Ellen Spertus


People also ask

Is System Out println thread safe?

println(String) is thread safe. Anyone's opinion and experience are very welcomed. the method AReallyLongTrickMethod() will be called, and will return, before System.

Can threads be interleaved?

Interference happens when two operations, running in different threads, but acting on the same data, interleave. This means that the two operations consist of multiple steps, and the sequences of steps overlap.

Is multi threading possible in Java?

Java is a multi-threaded programming language which means we can develop multi-threaded program using Java.

What is multi Threadingin Java?

In Java, Multithreading refers to a process of executing two or more threads simultaneously for maximum utilization of the CPU. A thread in Java is a lightweight process requiring fewer resources to create and share the process resources.


2 Answers

The OpenJDK source code answers your question:

public void println(String x) {     synchronized (this) {         print(x);         newLine();     } } 

Reference: http://hg.openjdk.java.net/jdk6/jdk6/jdk/file/39e8fe7a0af1/src/share/classes/java/io/PrintStream.java

like image 20
twimo Avatar answered Sep 20 '22 19:09

twimo


Since the API documentation makes no mention of thread safety on the System.out object nor does the PrintStream#println(String) method you cannot assume that it is thread-safe.

However, it is entirely possible that the underlying implementation of a particular JVM uses a thread-safe function for the println method (e.g. printf on glibc) so that, in reality, the output will be guaranteed per your first example (always ABC\n then ABC\n, never interspersed characters per your second example). But keep in mind that there are lots of JVM implementations and they are only required to adhere to the JVM specification, not any conventions outside of that spec.

If you absolutely must ensure that no println calls will intersperse as you describe then you must enforce mutual exclusion manually, for example:

public void safePrintln(String s) {   synchronized (System.out) {     System.out.println(s);   } } 

Of course, this example is only an illustration and should not be taken as a "solution"; there are many other factors to consider. For example, the safePrintln(...) method above is only safe if all code uses that method and nothing calls System.out.println(...) directly.

like image 99
maerics Avatar answered Sep 19 '22 19:09

maerics