In ArrayBlockingQueue, why copy final member field into local final variable?

后端 未结 2 688
借酒劲吻你
借酒劲吻你 2020-11-22 04:56

In ArrayBlockingQueue, all the methods that require the lock copy it to a local final variable before calling lock().

         


        
2条回答
  •  礼貌的吻别
    2020-11-22 05:17

    This thread gives some answers. In substance:

    • the compiler can't easily prove that a final field does not change within a method (due to reflection / serialization etc.)
    • most current compilers actually don't try and would therefore have to reload the final field everytime it is used which could lead to a cache miss or a page fault
    • storing it in a local variable forces the JVM to perform only one load

提交回复
热议问题