2 Answers
2

This means you're only going to have one static class SingletonHolder and only one INSTANCE no matter what the generic is.

To get past this, you could have 'multiple singletons' if that makes sense. You would have to add some kind of registration, so if you tried to get one that didn't exist yet, create and store it before returning it. Note this isn't compiled, and there may be some issues with it, but the basic approach is still the same.

If this is needed frequently, we can move the covariance casting to getInstance() for easy use:

Worker<Thread> worker = Listener.getInstance();
public final class Listener implements Worker<Runnable>
static Listener instance = new Listener();
// it is safe to cast Worker<A> to any Worker<B>
// as long as B is subtype of A
@SuppressWarnings("unchecked")
static public <T extends Runnable> Worker<T> getInstance()
return (Worker<T>)(Worker)instance;