Java - Immutable array thread-safety

后端 未结 4 1914
抹茶落季
抹茶落季 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 13:59

    I do think that you're afforded the same semantics with an array as with a final reference to an object. The spec states

    A thread that can only see a reference to an object after that object has been completely initialized is guaranteed to see the correctly initialized values for that object's final fields.

    It also says

    It will also see versions of any object or array referenced by those final fields that are at least as up-to-date as the final fields are.

    http://java.sun.com/docs/books/jls/third_edition/html/memory.html#17.5

提交回复
热议问题