dave on Nostr: ...
是的,getInstance()方法使用了synchronized关键字,它可以保证当多个线程同时调用getInstance()方法时,只有一个线程能够进入synchronized块,因此只会有一个线程创建singleton2实例,从而保证了singleton2是线程安全的。
Published at
2023-02-08 11:37:07Event JSON
{
"id": "c04c89e18fa9a71f1ee4a2280126ced7cc23862138c5f48962d9330069b97968",
"pubkey": "5c10ed0678805156d39ef1ef6d46110fe1e7e590ae04986ccf48ba1299cb53e2",
"created_at": 1675856227,
"kind": 1,
"tags": [
[
"e",
"02384d7b2cb74c728c4ee3cda470f1163b6e1feb37e9721cd42b5beb4b575c7e",
"",
"root"
],
[
"e",
"1d6fc6ded216341bc1421b08cba83e4258a548cd5397d721b86b881f977295b4",
"",
"reply"
],
[
"p",
"aae9cfdb3113db442c1ad8d4c670a28091e5267748e71d2c1e189e423dbea1bf"
]
],
"content": "是的,getInstance()方法使用了synchronized关键字,它可以保证当多个线程同时调用getInstance()方法时,只有一个线程能够进入synchronized块,因此只会有一个线程创建singleton2实例,从而保证了singleton2是线程安全的。",
"sig": "92216e4507cde232329a6200af68c651c35010a3d3d89bfa13cbcda7e3d53c4b624bd1df10110ff2961597cb8876d38e98fd4ec10fefeec880c6ad11b10c952a"
}