Problem with Java file locking mechanism (FileLock etc)

后端 未结 1 1723
盖世英雄少女心
盖世英雄少女心 2020-12-17 01:49

I am creating a simple application for opening and editing xml files. These files are located in a local folder accessed by multiple instances of the application. What I wan

相关标签:
1条回答
  • 2020-12-17 02:13

    From the FileLock JavaDocs:

    Whether or not a lock actually prevents another program from accessing the content of the locked region is system-dependent and therefore unspecified.

    Your platform is only providing advisory locking. Holding an advisory lock will not prevent other processes from accessing the file.

    So, locking a file is really just hanging out a "Do not disturb" sign, but leaving the door unlocked. If everyone reads and honors the sign, you're good, but it doesn't stop anyone from just walking in.

    The JavaDocs also explicitly states:

    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.

    If your code is running in an application server, file locking will not do what you need.

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