Java - Immutable array thread-safety

后端 未结 4 1910
抹茶落季
抹茶落季 2021-02-08 13:30

I have a question regarding the Java Memory Model. Here is a simple class presenting the problem:

public class Immutable         


        
4条回答
  •  时光说笑
    2021-02-08 14:03

    your example is not quite right. in order to get the final field assurance, you need:

    public ImmutableIntArray() {
        int tmparray = new int[10];
        for (int i = 0; i < 10; i++) {
            tmparray[i] = i;
        }
        array = tmparray;
    }
    

提交回复
热议问题