Is a Collection threadsafe if it's only written in the constructor?

前端 未结 4 1346
攒了一身酷
攒了一身酷 2021-01-27 16:04

Assuming we had this class

final class Foo {
    private final Set bar = new HashSet<>();

    public Foo() {
        bar.add(\"one\");
              


        
4条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-27 16:46

    Your Foo and bar are actually immutable. it is thread-safe.

提交回复
热议问题