Java进程间互斥 [英] Java inter-process mutex

查看:194
本文介绍了Java进程间互斥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Java中实现某种进程间互斥.我正在考虑按照此中的建议使用FileLock API线程.我基本上将使用一个虚拟文件并将其锁定在每个进程中.

I need to implement some kind of inter-process mutex in Java. I'm considering using the FileLock API as recommended in this thread. I'll basically be using a dummy file and locking it in each process.

这是最好的方法吗?还是这样的东西内置在标准API中(我找不到).

Is this the best approach? Or is something like this built in the standard API (I can't find it).

有关更多详细信息,请参见以下内容:

我编写了一个应用程序,该应用程序读取一些输入文件并根据在其中找到的内容更新一些数据库表(这比较复杂,但是此处的业务逻辑无关紧要).

I have written an application which reads some input files and updates some database tables according to what it finds in them (it's more complex, but business logic is irrelevant here).

我需要确保多个数据库更新之间的互斥.我尝试使用LOCK TABLE来实现此功能,但是我所使用的引擎不支持此功能.因此,我想在应用程序代码中实现锁定支持.

I need to ensure mutual exclusion between multiple database updates. I tried to implement this with LOCK TABLE, but this is unsupported by the engine I'm using. So, I want to implement the locking support in the application code.

推荐答案

我采用了FileLock API方法,并基于以下内容实现了一个简单的互斥体:

I went for the FileLock API approach and implemented a simple mutex based on:

  • FileChannel.lock
  • FileLock.release

所有进程都使用相同的虚拟文件来获取锁.

All the processes use the same dummy file for acquiring locks.

这篇关于Java进程间互斥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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