Java: Final field freeze on object reachable from final fields

前端 未结 5 625
不知归路
不知归路 2021-01-02 03:25

DZone refcard titled \"Core Java Concurrency\" states:

Once set, final field values cannot be changed. Marking an object reference field as final do

5条回答
  •  暖寄归人
    2021-01-02 04:10

    Does this mean that if I have a final field in class A of type Class B, which in turn have a final field of type Integer, then final field freeze for an instance of class A completes only after the final field freeze for b.c have already happened?

    I think I would carefully say that final field freeze in this case means that when you create an instance of A and safely publish it, other objects will never see an uninitialized value for b or c.

    I would also say that when you are creating the instance of B inside A, other initialization code inside A will never see an uninitialized value for c.

    One case where I have encountered real questions around final field freeze is for example a class that contains a (mutable) HashMap, intended only for read, initialized during construction:

    public class DaysOfWeek {
        private final Map daysOfWeek = new HashMap();
        public DaysOfWeek() { 
          // prepopulate my map
          daysOfWeek.put(0, "Sunday");
          daysOfWeek.put(1, "Monday");
          // etc
        }
    
        public String getDayName(int dayOfWeek) {
          return daysOfWeek(dayOfWeek);
        }
    }
    

    The question arises here: assuming this object is safely published, and given that there is no synchronization here, is it safe for other threads to call getDayName()? The answer is yes, because final field freeze guarantees that the HashMap and everything reachable from it (here it's just strings, but could be arbitrarily complex objects) is frozen at the end of construction. [If you want to actually modify this map after construction, then you'll need explicit synchronization around reads and writes.] Here's a lengthier blog exploring the topic and check the comments for some interesting responses by people like Brian Goetz.

    btw I'm the author of the refcard

提交回复
热议问题