跨几个jvm加锁?
这与这个问题有点相关。
我正在使用 make 来提取有关某些 C 程序的一些信息。我使用 bash 脚本来包装编译,该脚本运行我的 java 程序,然后运行 gcc。基本上,我正在做:
make CC=~/my_script.sh
我想使用多个作业(带有 make 的 -j 选项)。它根据依赖性规则运行多个进程。
如果我理解得很好,我将拥有与作业一样多的 jvm 实例,对吧?
问题是我正在使用 sqlite-jdb 来收集一些信息。那么问题是如何避免多个进程同时尝试修改数据库? 看来sqlite锁是jvm相关的(我的意思是一个锁只能在锁定jvm内部“看到”),并且这对于RandomAccessFile.lock()来说是相同的。
你知道该怎么做吗? (创建一个 tmp 文件,然后查看它是否存在似乎是一种可能性,但可能很昂贵。dB 中的锁定表?)
谢谢
this is a bit related to this question.
I'm using make to extract some information concerning some C programs. I'm wrapping the compilation using a bash script that runs my java program and then gcc. Basically, i'm doing:
make CC=~/my_script.sh
I would like to use several jobs (-j option with make). It's running several processes according to the dependency rules.
If i understood well, I would have as many instances of the jvm as jobs, right ?
The thing is that i'm using sqlite-jdb to collect some info. So the problem is how to avoid several processes trying to modify the db at the same time ?
It seems that the sqlite lock is jvm-dependant (i mean one lock can be "see" only inside the locking jvm), and that this is the same for RandomAccessFile.lock().
Do you have any idea how to do that ? (creating a tmp file and then looking if it exists or not seems to be one possibility but may be expensive. A locking table in the dB ? )
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
java.nio.channels.FileLock< /code>
允许操作系统级跨进程文件锁定。
然而,在调用 gcc 之前使用 make 启动并行运行多个 JVM 的 bash 脚本对我来说听起来太鲁布戈德堡式的而且脆弱。
java.nio.channels.FileLock
allows OS-level cross-process file locking.However, using make to start a bash scripts that runs several JVMs in parallel before calling gcc sounds altogether too Rube-Goldbergian and brittle to me.
对此有几种解决方案。
如果你的锁应该在同一台机器内,你可以使用服务器套接字来实现它(设法绑定到端口的进程首先拥有锁,其他进程等待端口变得可用)。
如果您需要跨多台计算机的锁,您可以使用 memcached 锁。这需要运行 memcached 服务器。如果您对此解决方案感兴趣,我可以粘贴一些代码。
您可以在此处获取连接到memcached的Java库。
there are several solutions for this.
if your lock should be within the same machine, you can use a server socket to implement it (The process that manages to bind to the port first owns the lock, other processes waits for the port to become available).
if you need a lock that span across multiple machines you can use a memcached lock. this will require a memcached server running. I can paste some code if you are interested in this solution.
you can get Java library to connect to memcached here.
您可以尝试使用 Terracotta 在不同 JVM 实例之间共享对象。对于您的需求来说,它可能看起来过于沉重,但至少值得考虑。
You may try Terracotta for sharing objects between various JVM instances. It may appear as a too heavy solution for your needs, but at least worth considering.