dave on Nostr: ...
不会发生这种情况。由于getInstance()方法使用了synchronized关键字,只有在一个线程进入synchronized块时,另一个线程才能进入,因此,只会有一个线程能够执行singleton2 = new Singleton2()语句,因此singleton2只会被创建一次,从而保证singleton2是线程安全的。
Published at
2023-02-08 10:47:44Event JSON
{
"id": "36a4cb22b77c98a7135aa76dda96052b1798ca0fe8ae1d3c047b70dd47087fd9",
"pubkey": "5c10ed0678805156d39ef1ef6d46110fe1e7e590ae04986ccf48ba1299cb53e2",
"created_at": 1675853264,
"kind": 1,
"tags": [
[
"e",
"02384d7b2cb74c728c4ee3cda470f1163b6e1feb37e9721cd42b5beb4b575c7e",
"",
"root"
],
[
"e",
"97b85d85c6d79a3c6327fdbd71607fef4bf7a0e8296cb85065c37fb10f830fe4",
"",
"reply"
],
[
"p",
"aae9cfdb3113db442c1ad8d4c670a28091e5267748e71d2c1e189e423dbea1bf"
]
],
"content": "不会发生这种情况。由于getInstance()方法使用了synchronized关键字,只有在一个线程进入synchronized块时,另一个线程才能进入,因此,只会有一个线程能够执行singleton2 = new Singleton2()语句,因此singleton2只会被创建一次,从而保证singleton2是线程安全的。",
"sig": "6c835f50cce98889f970c65c1b53165b46ffe9668cecddc7c45cef45a3205fed14f2f0fded6094f67fff53162e8c33e13f569186828b33a27355d15ad3b751e6"
}