File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes Is System.out.println(String) Thread safe? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Is System.out.println(String) Thread safe?" Watch "Is System.out.println(String) Thread safe?" New topic

Is System.out.println(String) Thread safe?

liang gu
Ranch Hand

Joined: Nov 05, 2001
Posts: 89
Thanks in advance
Leslie Chaim
Ranch Hand

Joined: May 22, 2002
Posts: 336
What can be depicted from this code ...

When called as:
java t
and output:

Thread-3 Hello
Thread-6 Hello
Thread-8 Hello
Thread-3 world
Thread-1 Hello
Thread-1 world
Thread-5 Hello
Thread-10 Hello
Thread-6 world
Thread-2 Hello
Thread-8 world
Thread-4 Hello
Thread-5 world
Thread-10 world
Thread-2 world
Thread-4 world
Thread-9 Hello
Thread-9 world
Thread-7 Hello
Thread-7 world

I guess I may clarify your question here, I am not exectly sure what you had in mind
Hope this helps

Normal is in the eye of the beholder
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
It seems that PrintStreams are indeed thread-safe, in the sense that most critical method s are synchronized. So if different threads call println() at (nearly) the same time, one whole line will print for one thread, and then the other. So you'll always get stuff like
Thread 1: abcdefghijklm
Thread 1: abcdefghijklm
but not
Thread 1: abcThread 2: ABCDEFGHIJKLM
Of course if you use print() rather than println() you may see mixed lines like the above. And beware that you will also see the above type of mixing if one message goes to System.out and another goes to System.err - they're separate in Java, but get interlaced on your screen output. Can be confusing.
Also, I can't find any details about thread safety of Ssytem.out or other PrintStreams in the API - I only know from examing ing the PrintStream source code. So that means there are really no guarantees, as other JDK implementations are free to do this differently. Also, I didn't check every single method to make sure it really was safe - I just saw that most methods were synchronized, and assumed that the folks at Sun were smart enought to do it safely. Then again, maybe they were smart enough to avoid documenting thread safety here because they knew it was impossible. Who knows? I guess fundamentally, if you need to guarantee thread safety, you have to do it yourself here. Or use alternate mechanisms like the java.util.logging classes, which do claim to be thread-safe in their APIs.

"I'm not back." - Bill Harding, Twister
liang gu
Ranch Hand

Joined: Nov 05, 2001
Posts: 89
First, thanks for your reply and detailed answer. However, I still have a little bit doubt.
1) I didn't find the class java.util.logging
2) If System.out.println(String) is indeed thread safe, then how about the following state:
System.out.println((String) AReallyLongTrickMethod());
You can virtually put anything in AReallyLongTrickMethod(). If the whole println() is synchronized, it could cause some problem. And this is the major reason I doubt that System.out.println(String) is thread safe.
Anyone's opinion and experience are very welcomed.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
In the statement
System.out.println((String) AReallyLongTrickMethod())
the method AReallyLongTrickMethod() will be called, and will return, before System.out.println() is called.

Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Ron's right, of course. And java.util.logging is part of J2SDK 1.4. You could also try other logging packages like Log4J instead if you prefer.
I agree. Here's the link:
subject: Is System.out.println(String) Thread safe?
It's not a secret anymore!