Restrict multiple instances of an application in java

前端 未结 8 1114
温柔的废话
温柔的废话 2020-12-03 22:55

I want to prevent multiple instances of application being launched in java. I know 2 methods for this:

  1. locking file
  2. locking socket

But

8条回答
  •  被撕碎了的回忆
    2020-12-03 23:12

    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.

提交回复
热议问题