If you use Mutex, it is logical that Mutex should be accessible from any JVM on which a copy of the "program" was running. In C programming, this can be done via shared memory, but by default Java does not.
With this understanding, there are many ways to realize what you want. You can open the server socket on the designated port (the operating system ensures that only one process is the recipient of the server socket, and subsequent ones fail).
You can use a βlock fileβ, but itβs a bit complicated, since the file you need to use will really be a directory (and it greatly depends on whether the creation of the directory is atomic for your file system, although most of the creation of the directory). If the system administrator decides to run you through NFS, things get even more complicated (if not impossible).
You can also do some great tricks with the JVM and debugging / JMI if you can somehow make sure that all the relevant JVMs start with the same configurations (over time, an impossible task).
Other people used the exec tool to execute the equivalent of a list of processes, but this is a bit complicated due to the possibility of a race condition (two processes check at the same time and cannot see each other).
In the end, the server socket route is probably the most stable, since it is guaranteed to communicate with only one process by the TCP / IP stack (and is mediated by the operating system). However, you will need to clear the socket of incoming messages, and this will open up the possibility of other security problems.
Edwin Buck May 9 '12 at 1:59 a.m. 2012-05-09 01:59
source share