2

Is the following method provided by Java NIO thread safe?

public static Path write(Path path,
     Iterable<? extends CharSequence> lines,
     Charset cs,
     OpenOption... options)
              throws IOException

The API doesn't say anything about the thread safe but inside the method implementation it uses a output stream that is thread as per the documentation. Also I have noticed that the method it invokes Writer.java-> write(String str, int off, int len) is having synchronized block.

There are some post related file writing API, but those are not using the NIO Files.write method.

Anyone can confirm whether the method is safe to be called from multiple threads?

Mayuran
  • 669
  • 2
  • 8
  • 39
  • @nullpointer, No, the suggested answer using different API – Mayuran Sep 19 '17 at 05:33
  • How about https://stackoverflow.com/a/9976133/1746118 in the same question? – Naman Sep 19 '17 at 05:38
  • @nullpointer, I am not looking for thread safe implementation, want to know the NIO method Files.write is thread safe or not – Mayuran Sep 19 '17 at 05:42
  • No it is not thread-safe. Nothing about it in the Javadoc, and a quick inspection of the code proves it. – user207421 Sep 19 '17 at 06:14
  • @Mayuran It is using a lower-level API. If the lower level isn't thread-safe, neither is the upper level. – user207421 Sep 19 '17 at 06:15
  • @EJP a more thorough inspection, and a test, however, prove it is currently thread-safe – bowmore Sep 19 '17 at 15:18
  • @bowmore, while checking the code, for me it seems like thread safe, I just want to check with some experts. That’s why posted here – Mayuran Sep 20 '17 at 01:09
  • @bowmore My thorough inspection of this ten-line method revealed that (a) the method itself is not synchronized; (b) it iterates over a collection of lines; and (c) it calls a synchronized method per line. There is therefore the possibility of line interleaving from a concurrent call. – user207421 Sep 20 '17 at 02:02
  • @EJP it's deceptive, the thing is the implementation uses `Files.newOutputStream()`, which is documented to be thread safe. You'd think that the for loop, would then still allow for interleaved lines in the file. However, some experimenting showed that until one `OutputStream` is closed, others have to wait. And thus interleaving of lines is also prevented. But, as `Files.write()` isn't documented to be thread safe, it would be unwise to rely on this to remain true in future releases. – bowmore Sep 20 '17 at 05:28
  • @bowmore 1. The synchronized output stream is wrapped in a non-synchronized `OutputStreamWriter`, which in turn is wrapped in a non-synchronized `BufferedWriter`. 2. Successive writes by `Files.write()` to the same underlying stream or writer are not thread-safe even if the individual writes are, unless the write loop in `Files.write()` itself is synchronized, which it isn't. The possibility of interleaved writes therefore exists. – user207421 Sep 22 '17 at 05:52
  • @EJP just found why I couldn't break it, the `BufferedWriter` saved up all output from a thread, to just flush it all in one go upon closing the writer. Once I increased my output, so the buffer couldn't contain it all, I was able to get interleaved lines. So yeah : not thread safe. – bowmore Sep 22 '17 at 06:38
  • @bowmore It isn't thread-safe regardless of buffering. This is the same issue that resulted in the Collections API not being synchronized. Synchronizing all the methods doesn't synchronize the iterators, and once you accept the necessity for synchronizing around iterations you may as well transfer responsibility for *all* synchronization to the caller. – user207421 Sep 24 '17 at 09:40
  • @EJP I'm not saying the buffering plays a role in the thread safety, I'm saying the buffering is what threw me in my test. – bowmore Sep 24 '17 at 10:36
  • @EJP Thanks a lot for your comments. So I conclude it is not thread safe. I have to handle from my application side. – Mayuran Sep 25 '17 at 02:55
  • @bowmore Thanks a lot for your comments. So I conclude it is not thread safe. I have to handle from my application side. – Mayuran Sep 25 '17 at 02:55

0 Answers0