如何实现java程序_如何实现单实例Java应用程序?

如果我相信这

article,通过:

having the first instance attempt to open a listening socket on the localhost interface. If it’s able to open the socket, it is assumed that this is the first instance of the application to be launched. If not, the assumption is that an instance of this application is already running. The new instance must notify the existing instance that a launch was attempted, then exit. The existing instance takes over after receiving the notification and fires an event to the listener that handles the action.

注意:Ahe在评论中提到使用InetAddress.getLocalHost()可能很棘手:

it does not work as expected in DHCP-environment because address returned depends on whether the computer has network access.

Solution was to open connection with InetAddress.getByAddress(new byte[] {127, 0, 0, 1});

Probably related to 07002.

>我还发现bug 4665037报告比getLocalHost的预期结果:返回机器的IP地址,实际结果:返回127.0.0.1。

it is surprising to have getLocalHost return 127.0.0.1 on Linux but not on windows.

The getMonitoredVMs(int processPid) method receives as parameter the current application PID, and catch the application name that is called from command line, for example, the application was started from c:\java\app\test.jar path, then the value variable is “c:\\java\\app\\test.jar“. This way, we will catch just application name on the line 17 of the code below.

After that, we search JVM for another process with the same name, if we found it and the application PID is different, it means that is the second application instance.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值