Concurrent file write in Java on Windows

前端 未结 4 2118
谎友^
谎友^ 2020-12-14 09:59

What happens when you concurrently open two (or more) FileOutputStreams on the same file?

The Java API says this:

Some platforms, in particula

4条回答
  •  抹茶落季
    2020-12-14 10:11

    I would be wary of letting the OS determine file status for you (since this is OS-dependent). If you've got a shared resource I would restrict access to it using a Re-entrant lock

    Using this lock means one thread can get the resource (file) and write to it. The next thread can check for this lock being held by another thread, and/or block indefinitely until the first thread releases it.

    Windows (I think) would restrict two processes writing to the same file. I don't believe Unix would do the same.

提交回复
热议问题