JVM对类中的最终变量做了什么

JVM对类中的最终变量做了什么

本文介绍了JVM对类中的最终变量做了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JVM如何在不同方面处理最终变量?

How does the JVM handle final variables differently under the hood?

推荐答案

JVM规范中至少有一节关于 final 对内存模型的影响,对于多线程代码来说非常重要:

There is at least one section in the JVM specification about final's impact on the memory model, and it is quite important for multi-threaded code:

final 对象的字段允许:


  • 当构造函数退出时,所有 final 字段必须是可见的

  • 通过 final 引用访问的任何对象上的字段也保证至少是最新的当构造函数退出时。

  • 总之,这意味着不可变对象(所有字段都是 final 并且是基元或引用的对象)可以在没有同步的情况下同时访问不可变对象。通过 final final ,但实际上永远不会更改)也是安全的/ code> reference。

  • 相反:如果您的对象被多个线程访问,并且您没有声明其字段 final ,那么你必须通过其他方式提供线程安全。

  • When a constructor exits, all final fields must be visible to all threads.
  • The fields on any object accessed via a final reference are also guaranteed to be at least as up to date as when the constructor exits.
  • Taken together, this means that immutable objects (ones where all fields are final and are either primitives or references to immutable objects) can be concurrently accessed without synchronization. It is also safe to read "effectively immutable" objects (ones whose fields aren't actually final, but in practice never change) via a final reference.
  • Conversely: If your object is accessed by multiple threads, and you don't declare its fields final, then you must provide thread-safety by some other means.

但请注意,JVM并未强制执行实际结束:您可以使用反射重新分配值(这当然会破坏安全发布)。

But note that the JVM does not enforce actual finality: You can re-assign values using reflection (which of course undermines the safe publication).

这篇关于JVM对类中的最终变量做了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 08:23