What will happen if two different programs try to write to the same file simulatneously?

StackOverflow https://stackoverflow.com/questions/5374567

  •  28-10-2019
  •  | 
  •  

  1. What will happen if two different programs try to write to the same file simultaneously?

  2. Will one of the programs experience a file lock error?

  3. How should programs be designed to handle this scenario?

有帮助吗?

解决方案

When the second app (or thread) try to open the file for writing it would throw IO exception.. simple..

其他提示

say you have user A and user B, what you can do is, let both of them modify the content, there will be a small difference in time however "simultaneous" you want it to be, so check which user has "submitted" the changes first, save those changes and prompt a smart message to the next user saying "file has been updated, check changes before.. blah blah".

Use FileLock to avoid IO Exception when file is being accessed by multiple threads.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top