我正在尝试同步一个像这样的 var 实例化:
Object o = new Object();
String s = null;
void getS() {
if (s != null) {
return s;
}
// multiple threads stopping here
// maybe using readwritelock? write.lock?
syncronize(o) {
// if previous thread stopped by sync block
// completed before, bypass this
if (s != null) {
return s;
}
// no one before, instantiate s
s = "abc";
}
return s;
}
有没有更好的方法来处理 var 的单次实例化?也许使用锁?