Determine if only one instance is running: Difference between revisions
Content added Content deleted
(Found incorrectness.) |
(→{{header|Java}}: Fixed this example so it now compiles and runs properly.) |
||
Line 540: | Line 540: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
⚫ | |||
{{incorrect|Java|Doesn't compile.}} |
|||
⚫ | |||
import java.net.InetAddress; |
import java.net.InetAddress; |
||
import java.net.ServerSocket; |
import java.net.ServerSocket; |
||
import java.net.UnknownHostException; |
import java.net.UnknownHostException; |
||
⚫ | |||
public class SingletonApp |
public class SingletonApp |
||
{ |
{ |
||
private static final int PORT = 65000; // random large port number |
|||
private static ServerSocket s; |
|||
⚫ | |||
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) { |
|||
⚫ | |||
System.out.print("OK, only this instance is running"); |
|||
⚫ | |||
System.out.println(" but will terminate in 10 seconds."); |
|||
try { |
|||
try { |
|||
⚫ | |||
Thread.sleep(10000); |
|||
⚫ | |||
if (s != null && !s.isClosed()) s.close(); |
|||
⚫ | |||
} catch (Exception e) { |
|||
System.err.println(e); |
|||
⚫ | |||
} |
|||
⚫ | |||
} |
|||
} |
|||
} |
|||
// main() and rest of application... |
|||
}</lang> |
}</lang> |
||