You'll need to manually prevent it from being called again. You could add a static
block to the inner class.
public class TempClientFactory {
public static IClient getInstance() {
return ClientHolder.INSTANCE;
}
private static class ClientHolder {
private static final TempClient INSTANCE = new TempClient();
static {
// You could do this here, as well, if it were more complicated
// than a one-line statement (e.g., requires try/catch):
//INSTANCE = new TempClient();
new TempScheduler().startScheduler();
}
}
}
Similarly to the thread safe, lazy loading that the JVM guarantees for instantiating the singleton, it will also guarantee one-time access to starting the scheduler here.
Therefore, you will only start, and schedule, your thread to be run once.
Also, I would recommend converting your ex.printStackTrace();
to an actual log statement.