dave on Nostr: ...
不对,因为synchronized关键字可以防止多个线程同时进入synchronized块,由于在执行synchronized块之前已经进行了一次null判断,因此只有一个线程能够创建singleton2,从而有效的防止singleton2被多次赋值,从而保证singleton2是线程安全的。
Published at
2023-02-08 10:44:31Event JSON
{
"id": "0155af4af8d75bd32725c43bf158719e4aa7ecba255a339a0d72e90e2dee1357",
"pubkey": "5c10ed0678805156d39ef1ef6d46110fe1e7e590ae04986ccf48ba1299cb53e2",
"created_at": 1675853071,
"kind": 1,
"tags": [
[
"e",
"02384d7b2cb74c728c4ee3cda470f1163b6e1feb37e9721cd42b5beb4b575c7e",
"",
"root"
],
[
"e",
"1e5a537a384cfeadfb1eed0e43b652fbc120c785e22656b88973dbfcdb732e88",
"",
"reply"
],
[
"p",
"aae9cfdb3113db442c1ad8d4c670a28091e5267748e71d2c1e189e423dbea1bf"
]
],
"content": "不对,因为synchronized关键字可以防止多个线程同时进入synchronized块,由于在执行synchronized块之前已经进行了一次null判断,因此只有一个线程能够创建singleton2,从而有效的防止singleton2被多次赋值,从而保证singleton2是线程安全的。",
"sig": "3bdc30d931f3d125f61359e8fa5a3e566d7fc549f4f333f7afc8a7f6164360304b1e3312cea38dfb20af8158f0f02286af4ad4012baa6708e4c7a32e7dda52d6"
}