Whenever implementing a singleton, the getInstance()
method should be thread-safe.
e.g.,
public static synchronized Clients getInstance()
... or ...
private static final Object INSTANCE_LOCK = new Object();
public static Clients getInstance() {
synchronized(INSTANCE_LOCK) {
if(instance == null) instance = new Clients();
}
return instance;
}
Of course, if you're in fact executing this bit of code from two different programs rather than two different threads, you'll have two instances. I'm assuming the former, because the latter makes your question nonsensical.
I suppose I should explain why that's nonsensical.
When you execute a Java program with a main(String[] args)
method, all of your classes are loaded into the JVM. If you then execute another program, you get another JVM and another "copy" of all the associated classes. Thus, you have two separate singletons -- one for each program. Classes aren't shared between the two.