Unable to lock files on Linux using java.nio.channels.FileLock

后端 未结 6 1163
花落未央
花落未央 2020-12-11 20:34

I was creating an application in Java for which I want only one instance running. For this purpose I created a file and got a lock while my application is running.

I

6条回答
  •  温柔的废话
    2020-12-11 20:50

    Why don't you save the PID into a file, and instead of locking the file, verify if there's a process with that ID. If there is, and it's an instance of your application, you know it's already running.

    A socket might be a good idea as well, since you can use it to communicate to the running instance something.

    EDIT:

    Also, from FileLock's javadoc:

    Whether or not a lock actually prevents another program from accessing the content of the locked region is system-dependent and therefore unspecified.

提交回复
热议问题