java java中的最终变量和同步块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2101327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Final variable and synchronized block in java
提问by Sajad Bahmani
What is a final variable in Java? For example: if I write final int temp;in function what is the meaning of the final keyword?
Also, when would I want to use final variable (both as a class variable and as a function variable)?
Why must variables in a synchronized block be declared final?
Java中的final变量是什么?例如:如果我写final int temp;在 function 中 final 关键字的含义是什么?
另外,我什么时候想使用最终变量(既作为类变量又作为函数变量)?
为什么同步块中的变量必须声明为 final?
回答by Jon Skeet
Basically it just means you can't change the value. For instance variables, you have to assign any final variables once (and only once) in the constructor (or with a variable initializer). Synchronization is a pretty orthogonal concept.
基本上它只是意味着您无法更改该值。对于实例变量,您必须在构造函数中(或使用变量初始值设定项)一次(且仅一次)分配任何最终变量。同步是一个非常正交的概念。
The primary reason for making a localvariable final is so you can use it in an anonymous inner class... this has nothing to do with being in a synchronized block.
将局部变量设为 final 的主要原因是您可以在匿名内部类中使用它……这与在同步块中无关。
Final variables areuseful for immutable classes, admittedly - and immutability makes life easier in a multi-threaded environment - but that's the only relationship between the two that I can think of...
最后的变量是对一成不变的类有用,固然-和永恒性使生活更轻松在多线程环境-但是这两个是我能想到的之间的唯一关系...
EDIT: Wildwezyr's comment makes sense in terms of not changing the variable on which you are synchronizing. That would be dangerous, for the reasons he's given. Is that what you meant by "variable in synchronized block"?
编辑:Wildwezyr 的评论在不更改您正在同步的变量方面是有意义的。出于他给出的原因,那将是危险的。这就是“同步块中的变量”的意思吗?
回答by WildWezyr
Final variables and synchronized code blocks do have something in common... If you declare non-final variable aand then write synchronized (a) { System.out.println('xxx'); }you will get warning "Synchronization on non-final field" - at least in NetBeans.
最终变量和同步代码块确实有一些共同点......如果您声明非最终变量a然后写入,synchronized (a) { System.out.println('xxx'); }您将收到警告“非最终字段同步” - 至少在 NetBeans 中。
Why you should not be synchronizing on non-final field? Because if field value may change, then different threads may be synchronizing on different objects (different values of the field) - so there could be no synchronization at all (every thread may enter synchronized block at the same time).
为什么不应该在非最终字段上进行同步?因为如果字段值可能发生变化,那么不同的线程可能会在不同的对象上进行同步(字段的不同值)——所以可能根本没有同步(每个线程可能同时进入同步块)。
Look here for example of real-life trouble caused by synchronizing on non-final field: http://forums.sun.com/thread.jspa?threadID=5379204
在此处查看由在非最终字段上同步引起的实际问题示例:http: //forums.sun.com/thread.jspa?threadID=5379204
回答by extraneon
In addition to what Jon Skeet said, the value can't be changed but the contents may be changed.
除了 Jon Skeet 所说的,值不能改变,但内容可能会改变。
final Integer one = new Integer(1);
...
one = new Integer(2); // compile error
final List list = new ArrayList();
...
list = new ArrayList(); // compile error
list.add(1); // Changes list, but that's fine!
Also be aware that final and static final are not the same. final is within the scope of the instance, whereas static final is the same for all instances of a class (in other languages this could be called a constant).
还要注意 final 和 static final 是不一样的。final 在实例的范围内,而 static final 对于类的所有实例都是相同的(在其他语言中,这可以称为常量)。
Personally I think the advantage of final, even when not absolutely required to get your software working, is in the semantical meaning. It offers you the possibility to say to the compiler and the next person working on that codethat this variable is not meant to be changed, and that trying to change it could result in a bug.
就我个人而言,我认为 final 的优势在于语义含义,即使不是绝对需要让您的软件工作。它使您可以向编译器和下一个处理该代码的人说这个变量不打算更改,并且尝试更改它可能会导致错误。

