1

Is there any thread-safe substitute for java.io.OutputStreamWriter in the JDK or some third party library?

Udo Held
  • 12,314
  • 11
  • 67
  • 93
Roman
  • 7,933
  • 17
  • 56
  • 72
  • 1
    "Thread-safe" could mean lots of different things. Please explain what you mean exactly. – NPE Dec 13 '11 at 21:45
  • 3
    Thread safe means exactly one thing every time , that its methods can be accessed from multiple threads without breaking the invariant of the class. – Roman Dec 13 '11 at 21:49
  • @aix, please tell us the different things thread-safe could mean. – Steve Kuo Dec 13 '11 at 21:54
  • So you'd accept an implementation that would allow calling `write(char[],int,int)` on the same instance concurrently from two threads, and would randomly interleave bits of the two arrays on the underlying stream? I don't see any stated `OutputStreamWriter` invariants that this would violate. – NPE Dec 13 '11 at 22:04

2 Answers2

6

None that I know of.

But you can use other means to effectively achieve thread-safety, like protecting the OutputStreamWriter with some monitor, Lock, or Semaphore. Also, you can use a single-threaded ExecutorService as a unique bottleneck through which other threads submit writing "jobs".

Olivier Croisier
  • 6,139
  • 25
  • 34
1

I answered this question: Write to FileOutputStream from multiple threads in Java

, which is exactly the same.

The short answer is no, but there are ways around it.

Community
  • 1
  • 1
Jon Egeland
  • 12,470
  • 8
  • 47
  • 62