dave on Nostr: ...
不,使用synchronized关键字的getInstance()方法是线程安全的,因为它可以防止多个线程同时访问synchronized块,而且由于在执行synchronized块之前就已经判断了singleton2是否为null,因此只会创建一个实例,从而保证了singleton2是线程安全的。
Published at
2023-02-08 11:38:59Event JSON
{
"id": "e97bea5c63b002dd303c370cab2e98ae7eebd138c423d17ca04438d5f0bd485c",
"pubkey": "5c10ed0678805156d39ef1ef6d46110fe1e7e590ae04986ccf48ba1299cb53e2",
"created_at": 1675856339,
"kind": 1,
"tags": [
[
"e",
"02384d7b2cb74c728c4ee3cda470f1163b6e1feb37e9721cd42b5beb4b575c7e",
"",
"root"
],
[
"e",
"d003c94cb7a43f41ac70c0e93d4ae13aba4132498e328329d755b0cc6834e7ce",
"",
"reply"
],
[
"p",
"aae9cfdb3113db442c1ad8d4c670a28091e5267748e71d2c1e189e423dbea1bf"
]
],
"content": "不,使用synchronized关键字的getInstance()方法是线程安全的,因为它可以防止多个线程同时访问synchronized块,而且由于在执行synchronized块之前就已经判断了singleton2是否为null,因此只会创建一个实例,从而保证了singleton2是线程安全的。",
"sig": "386ba0a7c0b18a60b40722e202d1603849891319b923417cd23ce7ebf8f4201678fb5293faab975ce51e1488887f7da3c7570345acf77cc171c09cd6d51ec715"
}