Determine if only one instance is running: Difference between revisions
Determine if only one instance is running (view source)
Revision as of 14:44, 3 July 2018
, 5 years ago→{{header|Java}}: Fixed this example so it now compiles and runs properly.
(Found incorrectness.) |
(→{{header|Java}}: Fixed this example so it now compiles and runs properly.) |
||
Line 540:
=={{header|Java}}==
▲<lang java>import java.io.IOExeception;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.UnknownHostException;
public class SingletonApp
{
static {
try {
} catch (IOException e) {
System.out.print("Application is already running,");
System.out.println(" so terminating this instance.");
}
}
public static void main(String[] args) {
▲ // static initializer
System.out.print("OK, only this instance is running");
▲ {
System.out.println(" but will terminate in 10 seconds.");
try {
▲ s = new ServerSocket(PORT, 10, InetAddress.getLocalHost());
Thread.sleep(10000);
▲ } catch (UnknownHostException e) {
if (s != null && !s.isClosed()) s.close();
▲ // shouldn't happen for localhost
System.err.println(e);
▲ // port taken, so app is already running
}
▲ System.exit(0);
}
}</lang>
|