chenzhiguo on Nostr: class Singleton2 { private static volatile Singleton2 singleton2; private ...
class Singleton2 {
private static volatile Singleton2 singleton2;
private Singleton2(){};
public static Singleton2 getInstance() {
if (singleton2 != null) {
return singleton2;
}
synchronized (Singleton2.class) {
singleton2 = new Singleton2();
}
return singleton2;
}
}
这个线程安全吗?
Published at
2023-02-08 09:04:41Event JSON
{
"id": "4e9b01e9eb663023122f20422aab51815b2199c59a4823ceb8e1aee460db209c",
"pubkey": "aae9cfdb3113db442c1ad8d4c670a28091e5267748e71d2c1e189e423dbea1bf",
"created_at": 1675847081,
"kind": 1,
"tags": [
[
"e",
"02384d7b2cb74c728c4ee3cda470f1163b6e1feb37e9721cd42b5beb4b575c7e",
""
],
[
"e",
"d52a51201389dfce5e3d74edf4e7cb34329fe93685b2e1b225cb46e344080fdb"
],
[
"p",
"5c10ed0678805156d39ef1ef6d46110fe1e7e590ae04986ccf48ba1299cb53e2"
]
],
"content": "class Singleton2 {\n private static volatile Singleton2 singleton2;\n\n private Singleton2(){};\n\n public static Singleton2 getInstance() {\n if (singleton2 != null) {\n return singleton2;\n }\n synchronized (Singleton2.class) {\n singleton2 = new Singleton2();\n }\n return singleton2;\n }\n}\n这个线程安全吗?",
"sig": "fc9e392b530dd32fbb04a3ade5a12cd9a926bd7bbe84d7ea95e525ab294124915b465593e80b11164310b200acc47fe684ea7d64e4720f203f7ebe57f2bd8c31"
}