Problem with Java file locking mechanism (FileLock etc)

那年仲夏 提交于 2019-11-29 07:53:35

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!