Having a single instance of an application is crucial in most of the software. In this article, I am giving an example on how to implement single instance of an Application.
How it works :
- New instance of application tries to connect to a specific ServerSocket (localhost, port#) to detect running applications. And a running application must have a ServerThread to detect possible run of new instance of the same application
- The main Logic in steps
- Find existing server socket running on localhost
- If found(another instance was already running) --> exit current instance of application
- else --
- start a new Server thread to detect run of future applications
- and start the application
Complete source code :