Multithreaded access to file

后端 未结 7 893
[愿得一人]
[愿得一人] 2020-12-30 08:04

We have a multi threaded java program. Multiple-threads will write to a file, and one thread will read from that file. I am looking for some design ideas. Is synchronization

相关标签:
7条回答
  • 2020-12-30 08:36

    Synchronization is necessary in this case. FileChannel is useful for preventing files being modified by processes outside the JVM: not so for applications which include multiple threads writing to a single file. From (further down in) the JavaDoc for FileChannel:

    File locks are held on behalf of the entire Java virtual machine. They are not suitable for controlling access to a file by multiple threads within the same virtual machine.

    See this post for a brief discussion of strategies to share file writing between threads.

    0 讨论(0)
提交回复
热议问题