Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
liang gu
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks in advance
 
Leslie Chaim
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 2: ABCDEFGHIJKLM
or
Thread 2: ABCDEFGHIJKLM
Thread 1: abcdefghijklm
but not
Thread 1: abcThread 2: ABCDEFGHIJKLM
defghijklm
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.
 
liang gu
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim:
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.
Liang
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the statement
System.out.println((String) AReallyLongTrickMethod())
the method AReallyLongTrickMethod() will be called, and will return, before System.out.println() is called.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic