Multithreaded access to file

后端 未结 7 892
[愿得一人]
[愿得一人] 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:13

    If being synchronous isn't important, you could have your writer running in its own thread, and allow other threads to queue up writes to the file. Although I think the first thing to consider is whether writing to a file is really what you want to do. Especially in high-traffic situations, having a lot of disk I/O may not be very efficient.

    0 讨论(0)
  • 2020-12-30 08:13

    If you wanted multiple readers and one writer, you would be looking for a Read Write Lock or a Read Write Mutex.

    But you want multiple writers and one reader. How do you know these writers won't overwrite each others data? Are they somehow segregated?

    0 讨论(0)
  • 2020-12-30 08:14

    I would consider synchronization in this case. Imagine that 2 threads (t1 and t2) open the file at the same time and start writing to it. The changes performed by the first thread are overwrited by the second thread because the second thread is the last to save the changes to the file. When a thread t1 is writing to the file, t2 must wait until t1 finishes it's task before it can open it.

    Also, if you care about the latest possible update of the file, you should synchronize the writing threads with the thread that reads the file so that if there's any thread writing the the file, the reading thread should wait.

    0 讨论(0)
  • 2020-12-30 08:16

    You need synchronization (locking) if you have a mix of readers and writers or writers and writers. If you only have readers, you don't need any synchronization.

    You don't want two processes writing to the same file or one process writing a file that another is reading.

    0 讨论(0)
  • 2020-12-30 08:21

    Once multiple Threads access shared data then Synchronization is necessary. If multiple threads write to the same file without some form of locking, then potentially you will end up with a lost update problem.

    Reading is not as big an issue in all circumstances so you need to consider...if a thread is reading the file and at the same time another thread updates the file, does the reading thread need to know about the change? If so you need to lock the file for the reading thread also.

    0 讨论(0)
  • 2020-12-30 08:30

    FileChannel is in theory thread safe. From the javadoc:

    File channels are safe for use by multiple concurrent threads. The close method may be invoked at any time, as specified by the Channel interface. Only one operation that involves the channel's position or can change its file's size may be in progress at any given time; attempts to initiate a second such operation while the first is still in progress will block until the first operation completes. Other operations, in particular those that take an explicit position, may proceed concurrently; whether they in fact do so is dependent upon the underlying implementation and is therefore unspecified.

    If you can use these, then you can use the built-in synchronization, rather than having to write your own.

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