多读取器文件java
我目前正在使用 Java 6 和 Spring 3。
我有一些进程正在读取同一文件,在本例中是 XML 文件。
另外,此过程需要更新该文件的内容,即我需要更新日期。
该进程同时运行,因此它们有可能同时读取并尝试写入。
我听说过 ReadWriteLock,它可以有多个读取器,但只能有 1 个写入器。 我现在正在考虑这一点。但由于进程运行速度很快,
会出现这样的情况:进程A当前正在写入更新,而进程B正在等待,另一个进程C正在等待写入更新。如果进程 B 正在更新相同的元素,则进程 B 可能只是旧更新。
有什么想法我该如何实现这一目标吗?
谢谢,
I'm currently using Java 6 with Spring 3.
I have some processes that are reading the same file, in this case is XML file.
Also, this processes need to update the content of that file which is I need to update a date.
The process run simultaneously, so there's a possibility they will read and try to write at the same time.
I heard about ReadWriteLock, which can have multiple reader and only 1 writer.
I'm considering that at the moment. But because the process is running quickyly,
There will be a scenario when process A is currently writing an update, and process B is waiting and another process C is waiting for writing the update. Process B might be just an old update if process B is updating the same element.
Is there any idea how can I achieve this?
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果所有“进程”都是线程,则使用同步。这正是这个目的。 ReadWriteLock 无论如何都不会保护文件本身;它用于协调对内存中对象的共享访问。
像这样的伪代码:
}
很多人会告诉您同步很慢,等等。除非您正在实现使用同步原语实现大规模并发的东西,否则效果会很好。如果需要,您可以随时使用 java.util.concurrent 进行优化。使用普通同步的简单性和可读性是值得的。如果您需要让其他进程访问数据,只需使用 RMI 或其他网络解决方案包装数据库类即可。
If all of the "processes" are threads, use synchronization. That is exactly what this is for. ReadWriteLock won't protect the file itself anyhow; it is for coordinating shared access to objects in memory.
Something like this pseudo-code:
}
A lot of people will tell you that synchronization is slow, etc. Unless you're implementing something that is meant to be massively concurrent using synchronization primitives will work fine. You can always optimize with java.util.concurrent later on if needed. The simplicity and readability of using plain synchronization is worth it. If you need to give other processes access to the data, just wrap the database class with RMI or some other networking solution.