Recommended Answers
Jump to PostBind a ServerSocket. If it fails to bind then abort the startup. Since a ServerSocket can be bound only once, only single instsances of the program will be able to run.
And before you ask, no. Just because you bind a ServerSocket, does not mean you are open to …
Jump to PostYou can use a lock file in your base directory as well. We use that here at work for one particular app that should only be running a single instance.
I hadn't thought about a socket binding though. Interesting alternative.
Jump to Postlockfiles are extremely risky.
What if the running instance of the application crashes (or the computer it's running on crashes which has the same effect)?The lockfile will still be there when you start it again, causing it to never start until the file is manually removed.
And what …
Jump to Postlockfiles are extremely risky.
What if the running instance of the application crashes (or the computer it's running on crashes which has the same effect)?The lockfile will still be there when you start it again, causing it to never start until the file is manually removed.
Yes, depending on …
Jump to PostGlad it works for you. Keep in mind the points jwenting mentioned though. I don't think you will have those issues with the code I pasted, but testing is your best friend.
Be sure you release the lock and close the channel when you no longer need them:
All 18 Replies
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.