Alex Rivera | Logout

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

Asked 2012-02-27T03:23:22.573
86

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
Edit
Report

1 Answer

3

Just to clarify, say you have two threads, one that prints "ABC" and another that prints "DEF". You will never get output like this: ADBECF, but you could get either

ABC
DEF 

or

DEF
ABC
answered 2012-02-27T04:04:06.380

Your Answer