Java文件锁定机制问题(FileLock等) [英] Problem with Java file locking mechanism (FileLock etc)

查看:168
本文介绍了Java文件锁定机制问题(FileLock等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个用于打开和编辑xml文件的简单应用程序。
这些文件位于由多个应用程序实例访问的本地文件夹中。
我想要做的是锁定由应用实例打开的每个文件,以便其他实例无法访问它。



为实现这一点我使用以下代码:



function void readFile(){

  File xmlFile = new File(myFile.xml); 
RandomAccessFile raf = new RandomAccessFile(xmlFile,rw);
FileLock fl = raf.getChannel()。tryLock();

if(fl == null){
System.out.println(文件已被另一个实例锁定);
} else {
setCurrentFile(raf);
setLock(fl);
System.out.println(此实例成功锁定文件);
}

}



由于我想保持对正在编辑的文件的锁定持续时间我不关闭raf也不释放fl。



此时任何其他实例尝试访问锁定文件的应用程序无法执行此操作。到目前为止一切都那么好。



我观察到以下奇怪的事情:



如果在获得锁定之后文件,我在同一个文件上打开一个FileInputStream,即使FileLock对象仍然有效(isValid返回true),该应用程序的其他实例现在可以访问正在编辑的文件。



<我觉得这个行为很奇怪。
有谁可以解释为什么会这样?



我希望上述内容有意义。
提前致谢!

解决方案

从FileLock JavaDocs:


锁是否实际阻止其他程序访问锁定区域的内容是系统相关的,因此未指定。


您的平台仅提供咨询锁定。持有咨询锁不会阻止其他进程访问该文件。



因此,锁定文件实际上只是挂出请勿打扰的标志,但是门打开了。如果每个人都阅读并尊重这个标志,你就会很好,但它不能阻止任何人进入。



JavaDocs也明确指出:


文件锁代表整个Java虚拟机。它们不适合控制同一虚拟机中多个线程对文件的访问。


如果您的代码在应用程序中运行服务器,文件锁定将无法满足您的需求。


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 want to do is lock each file that is opened by an instance of the app, so that other instances cannot access it.

To achieve this I use the following code:

function void readFile(){

   File xmlFile = new File("myFile.xml");
   RandomAccessFile raf = new RandomAccessFile(xmlFile, "rw");
   FileLock fl = raf.getChannel().tryLock();

   if(fl==null){
       System.out.println("file already locked by another instance");
   }else{
       setCurrentFile(raf);
       setLock(fl);
       System.out.println("file successfully locked by this instance");
   }  

}

Since I want to keep the lock on the file being edited for the duration I do not close the the raf nor release the fl.

At this point any other instance of the app that tries to access the locked file fails to do so. So far so good.

I have observed the following strange thing:

If after acquiring the lock on the file, I open a FileInputStream on the same file, even though the FileLock object remains valid (isValid returns true), other instances of the app can now access the file being edited.

I find this behaviour strange. Could anyone explain why this happens?

I hope the above make sense. Thanks in advance!

解决方案

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.

这篇关于Java文件锁定机制问题(FileLock等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆