java中限制一个应用程序的多个实例
我想防止在java中启动应用程序的多个实例。我知道有两种方法:
- 锁定文件
- 锁定套接字
但哪一种更有效且好用?我应该使用哪一个?
也欢迎任何其他解决方案来做同样的事情。
I want to prevent multiple instances of application being launched in java. I know 2 methods for this:
- locking file
- locking socket
But which is one is more efficient and good to use? Which one should I use?
Any other solution to do the same are also welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
有一个名为 jUnique 的库可以做到这一点,并且可以省去您自己实现它的麻烦。
There is a library called jUnique which does that and will save you the bother of implementing it yourself.
如果您使用 Java WebStart 进行部署,则 SingleInstanceService 会执行此操作。
请参阅 http://download.oracle。 com/javase/6/docs/technotes/guides/javaws/developersguide/faq.html#218
If you deploy with Java WebStart the SingleInstanceService does this.
See http://download.oracle.com/javase/6/docs/technotes/guides/javaws/developersguide/faq.html#218
编辑:我尝试使用 Win200864b(版本并不重要)和活动 JFrame 并移动到Front() 或使用 JFrame.DO_NOTHING_ON_CLOSE 在 SystemTray 中图标化
EDIT: I tried that with Win200864b(version isn't important) and alive JFrame and move toFront() or Iconified in SystemTray with JFrame.DO_NOTHING_ON_CLOSE
我的投票是锁定端口(我认为这就是你所说的套接字的意思)。我不知道具体原因。但事实上,我在大多数实际项目中只遇到过这种解决方案。不过我很高兴听到其他方法。
My vote goes to locking on a port (i think this is what you mean by socket). I don't know the exact reason for this. But in fact i come across only this as a solution in most practical projects. Though i will be happy to hear the alternative ways.
针对您的问题,端口解决方案将从机器上保留更多资源:
- 您将保持端口锁定:端口受到限制,您可能会发现防火墙或侦听同一端口的其他程序出现问题。
- 您需要一个活动线程。
文件解决方案将使用较少的机器资源,为了避免永远锁定文件,您需要添加一个线程,以删除文件,在 addShutdownHook 运行时方法。
In response to your question, the port solution will keep more resources from the machine:
- You will keep a port locked: ports are limited and you may find problems with firewalls or other programs listening on the same port.
- You'll need an active thread.
The file solution will use less resources from the machine, to avoid locking the file forever you need to add a thread, to delete the file, in the addShutdownHook method from Runtime.
Serversocket 解决方案是跨平台的。并且不会因为程序崩溃而无法重置锁。
the serversocket solution is cross-platform . And will not be vulnerable to the program crashing and not resetting the lock.
在我看来,文件锁定是更好的方法。当您在用户的主目录中创建文件时,这在多用户环境中仍然有效。
我遇到了 - JUnique - 还没有机会使用它
http://www.sauronsoftware.it/projects/junique/manual.php
File lock is better way to do- imo. When you create the file in User's Home directory, this will still work in a multi-user environment.
I came across - JUnique - haven't had a chance to use it
http://www.sauronsoftware.it/projects/junique/manual.php
我知道这个问题已经很老了,但我现在必须解决同样的问题。我更喜欢套接字解决方案,因为我从来没有想过这种任务应该与文件系统有任何关系。我认为最好在内存中解决问题,而不是在文件系统中。
I know that this question is pretty old, but I have to solve the same problem at the moment. I prefer the socket solution because I have never thought that such kind of task should have anything to do with the file system. It is better to solve the problem in memory and not in the file system I think.