次のような 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 の単一のインスタンス化を処理するより良い方法はありますか? たぶんロックを使用していますか?