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

后端 未结 4 923
青春惊慌失措
青春惊慌失措 2020-11-22 08:19

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

4条回答
  •  -上瘾入骨i
    2020-11-22 08:41

    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

提交回复
热议问题