Thread-safe iteration over a collection

后端 未结 8 1221
情歌与酒
情歌与酒 2020-12-05 05:21

We all know when using Collections.synchronizedXXX (e.g. synchronizedSet()) we get a synchronized \"view\" of the underlying collection.

Ho

8条回答
  •  星月不相逢
    2020-12-05 05:55

    Depends on your access model. If you have low concurrency and frequent writes, 1 will have the best performance. If you have high concurrency with and infrequent writes, 3 will have the best performance. Option 2 is going to perform badly in almost all cases.

    foreach calls iterator(), so exactly the same things apply.

提交回复
热议问题