Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is PrintWriter thread-safe?

Given the following:

public class CConsole {
  public static PrintWriter pw = new PrintWriter(System.out, true);
}

Is CConsole.pw.format("%d %d", x, y) thread-safe? That is, can multiple threads use this invocation and where is it described as being thread-safe. I don't see it in the PrintWriter class description nor the format() method description.

like image 695
H2ONaCl Avatar asked Jun 17 '12 14:06

H2ONaCl


People also ask

Is Java Println thread safe?

println(String) is thread safe. Anyone's opinion and experience are very welcomed. the method AReallyLongTrickMethod() will be called, and will return, before System.

Is PrintWriter faster than system out?

PrintWriter class is the implementation of Writer class. By using PrintWriter than using System. out. println is preferred when we have to print a lot of items as PrintWriter is faster than the other to print data to the console.

Is Bytearrayoutputstream thread safe?

Yes it is thread safe, or rather all its methods are synchronized, and ProcessBuilder.

Why do we use PrintWriter?

The main reason to use the PrintWriter is to get access to the printXXX methods (like println(int)). You can essentially use a PrintWriter to write to a file just like you would use System. out to write to the console.


1 Answers

It's not exactly the same question and there's no evidence given.

It also says PrintWriter is thread-safe without the qualification whereas another answer had the qualification. In my view this needs clarification.

Since the OP doesn't understand (or maybe doesn't believe) the answers to the linked question, I will restate them.

  • The primary specification (i.e. the javadocs) do not state whether or not the class is thread-safe.

  • It is clear from reading the source code that it is thread-safe in the sense that all relevant operations are properly synchronized.

  • It is unlikely that Oracle would deliberately change the implementation to make it non-thread-safe ... in the sense above.

  • However, there are use-cases where a PrintWriter may not be completely thread-safe:

    • If a single PrinterWriter is used by multiple threads, the result can be unpredictable interleaving of output from the threads; e.g. if they use print rather than println.

    • If you have multiple PrintWriter objects wrapping the same underlying stream, there could be problems due to the PrintWriters internal use of a BufferedWriter.

    • Things may change if you subclass PrintWriter.

In summary, the current PrintWriter implementation (in the Oracle/OpenJDK codebase) is thread-safe, but you still need to be careful in some situations. There is also the possibility other 3rd-party Java implementations (i.e. not derived from the OpenJDK source tree) might not be thread-safe.

Thread-safety of applications that involve multiple threads writing to the same underlying stream is always nuanced ...


Note that the quote that @KazekageGaara found ...

"All of the methods of PrintWriter that write multiple times to the underlying output stream handle synchronization internally, so that PrintWriter objects are thread-safe."

... is from an O'Reilly text book - "Java Fundamental Classes Reference" by Mark Grand and Jonathan Knudsen. Since this is not an official Sun / Oracle publication, it is not in any way definitive.

like image 81
Stephen C Avatar answered Sep 21 '22 20:09

Stephen C