Thursday, October 11, 2007

Note to self. synchronized singleton access

So, what's the best thread safe singleton instantiation.. I must look this up sometime.

Here's what we have now.

public static SingletonObject getInstance() {
if (instance == null) {
synchronized (SingletonObject.class) {
// might have been preempted just after first check
// but before the synchronized statement was executed
if (instance == null) {
instance = new SingletonObject();
}
}
}
return instance;
}

0 comments: