How to ensure only one instance of a java program can be exucuted
It is requited that only one instance of a JAVA program can be executed at
a certain time. I have observed a good number of solutions proposed in
stack overflow in some earlier posts. The solutions are based on:
1.By opening socket: To open a socket connection. 2.Based on the file lock
idea: To create a temporary file and to add a shutdown hook to unlock that
file when the JVM shuts down.
I do not want to use port locking as it may cause a possible conflict in
ports usage. So I was thinking to use file locking. Now after searching a
bit, I have found the proponents of the port locking based mechanism has
mentioned file locking can be unreliable if application crashed and for
other IO errors.
What I need is to find a solution which will work in cross platform. My
intended platform is Windows and Linux.
Can anyone shed some light on to this?
No comments:
Post a Comment