Linux-wide semaphore with a Java API?

You could create a 0 byte file 'methodname. LOCK' in a shared directory when you begin the operation, and delete it when you are done. Creating it with a no recreate flag in Java should solve your problem.

You could create a 0 byte file 'methodname. LOCK' in a shared directory when you begin the operation, and delete it when you are done. Creating it with a no recreate flag in Java should solve your problem.

Make sure to delete it in a 'finally' block so you are never left in a state where the file exists and continues to block the process from ever running.

1 +1 for lock files. You can use File.createTempFile() to create a temporary file. And calling deleteOnExit() on it will make sure it gets deleted when you app finishes.

– Axel Knauf Sep 25 at 13:14 This is a very good answer for the command line utility case. I also have a Swing program that calls my various "command line" utilities. I like how Java has multiple main() programs.In the Swing case, the life of the JVM will be longer so I cannot use the deleteOnExit() which is tied to the life of the JVM.

I decided to use createNewFile and manage deletion myself. – broiyan Sep 26 at 2:02.

Just open a serversocket at a specific port. If this succeeds there is no other application, if not an exception is thrown.

It works, but there is a small chance that this will interfere with some other application that needs to use that port. Also, you are using system resources by keeping that socket open. – Stephen C Sep 25 at 11:08.

The only standard Java feature I've seen that prohibits multiple instances of the same program is the SingleInstanceService in Java WebStart. download.oracle.com/javase/1.5.0/docs/gu... It requires you to create a JNLP file for your application, which is a bit cumbersome but may be very useful to you anyway. Note that Java does not allow command line parameters to be passed to your program which may be very inconvenient in this situation.

Depends on what your command line says. If Java WebStart does not work for you, then you must ask the operating system for a resource which is denied for subsequent requests and have it automatically released. The only one of these not platform specific is the TCP/IP socket but these are rare and may be used by another program.

Under Linux you may ask for shared memory but I do not have personal experience with this.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions