java进程同步,Java中的跨进程同步

在Java中无法直接实现类似Win32 Named Mutex的对象进行进程同步,但有两种替代方案。一是利用Socket或Channel进行通信,其中一个应用监听并等待信号,另一个应用发送完成信号。二是直接从Java中调用WinAPI来达到同步目的。这种方式适用于需要跨进程同步的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

How can I synchornize two Java processes running on Windows ?

I am looking for something like the Win32 Named Mutex object which allows two processes to use the same locking object.

Thanks

解决方案

It is not possible to do something like you want in Java. Different Java applications will use different JVM's fully separating themselves into different 'blackbox'es. However, you have 2 options:

Use sockets (or channels). Basically one application will open the listening socket and start waiting until it receives some signal. The other application will connect there, and send signals when it had completed something. I'd say this is a preferred way used in 99.9% of applications.

You can call winapi from Java. I do not remember the specifics, but you can find a plethora of example if you google "java winapi".

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值