Is multithreaded output alternating from System.out.println

If multiple threads call System.out.println (String) without synchronization, is it possible to interleave the output? Or is each row atomic? The API does not mention synchronization, so it seems possible, or is interlaced output prevented by buffering and / or virtual machine memory model, etc.?

EDIT:

For example, if each thread contains:

System.out.println("ABC"); 

Whether the conclusion will be guaranteed:

 ABC ABC 

or it could be:

 AABC BC 
+62
java multithreading synchronization
Feb 27 '12 at 3:23
source share
4 answers

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

However, it is possible that the base implementation of a specific JVM uses a thread-safe function for the println method (for example, printf in glibc ), so that in fact the output will be guaranteed in your first example (always ABC\n and then ABC\n , never unlabeled characters for your second example). But keep in mind that there are many JVM implementations, and they should only adhere to the JVM specification, and not any conventions outside of this specification.

If you absolutely must make sure that println calls do not intersect as they are described, you must force the mutual exception 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 described above is safe only if all the code uses this method and does not directly call System.out.println(...) .

+56
Feb 27 '12 at 4:00
source share

OpenJDK source code answers your question:

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

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

+17
May 6 '12 at 5:01
source share

Until you change the OutputStream through System.setOut , it is thread safe.

Although this is thread safe, you can have many threads writing to System.out to

 Thread-1 System.out.println("A"); System.out.println("B"); System.out.println("C"); Thread-2 System.out.println("1"); System.out.println("2"); System.out.println("3"); 

can read

 1 2 A 3 B C 

among other combinations.

So, to answer your question:

When you write to System.out - it gets a lock on the OutputStream instance - it will be written to the buffer and hidden right away.

As soon as it releases the lock, the OutputStream is cleared and written. There would be no instance in which you would have different lines, such as 1A 2B .

Edit the answer to your edit:

This will not happen with System.out.println . Since PrintStream synchronizes the entire function, it fills the buffer and then flushes it atomically. Any new incoming stream will now have a new buffer for operation.

+9
Feb 27 2018-12-12T00:
source share

To clarify, let's say you have two threads, one of which prints "ABC" , and the other - "DEF" . You will never get this output: ADBECF , but you can get

 ABC DEF 

or

 DEF ABC 
+2
Feb 27 2018-12-12T00:
source share



All Articles