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 08:52:30Event JSON
{
"id": "d8ad53171f6edcc1e23df4fed2199096d11d84711c7d210273a2c9943bcd87d8",
"pubkey": "aae9cfdb3113db442c1ad8d4c670a28091e5267748e71d2c1e189e423dbea1bf",
"created_at": 1675846350,
"kind": 1,
"tags": [
[
"e",
"02384d7b2cb74c728c4ee3cda470f1163b6e1feb37e9721cd42b5beb4b575c7e",
""
],
[
"e",
"e3fb35edef3df680edb8ee18a3c892da21b3f3f525531c97758f24bc621dde0f"
],
[
"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": "fc6faab285de30ef6e35a8d68cac08ff2612e895c182f3908ba1e2485747471494c4688ebc0fb1a3ffb4a16af98a8409f0a48c2898217fe7173aac669208c3b1"
}