简体   繁体   English

java中的最终变量和同步块

[英]Final variable and synchronized block in java

What is a final variable in Java? Java中的最终变量是什么? For example: if I write final int temp; 例如:如果我写final int temp; in function what is the meaning of the final keyword? 在函数中final关键字的含义是什么?
Also, when would I want to use final variable (both as a class variable and as a function variable)? 另外,我什么时候想使用final变量(作为类变量和函数变量)?
Why must variables in a synchronized block be declared final? 为什么同步块中的变量必须声明为final?

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 local variable final is so you can use it in an anonymous inner class... this has nothing to do with being in a synchronized block. 使局部变量最终的主要原因是你可以在匿名内部类中使用它...这与在同步块中无关。

Final variables are useful 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 . 编辑:Wildwezyr的评论在不改变您正在同步的变量方面是有意义的。 That would be dangerous, for the reasons he's given. 由于他给出的原因,这将是危险的。 Is that what you meant by "variable in synchronized block"? 这就是“同步块中的变量”的含义吗?

Final variables and synchronized code blocks do have something in common... If you declare non-final variable a and then write synchronized (a) { System.out.println('xxx'); } 最终变量和同步代码块确实有一些共同之处...如果你声明非最终变量a然后写synchronized (a) { System.out.println('xxx'); } synchronized (a) { System.out.println('xxx'); } you will get warning "Synchronization on non-final field" - at least in NetBeans. 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

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对于类的所有实例都是相同的(在其他语言中,这可以称为常量)。

Personally I think the advantage of final, even when not absolutely required to get your software working, is in the semantical meaning. 我个人认为final的优势,即使不是绝对需要让你的软件正常工作,也具有语义意义。 It offers you the possibility to say to the compiler and the next person working on that code that this variable is not meant to be changed, and that trying to change it could result in a bug. 它使您可以向编译器和处理该代码的下一个人说这个变量不应该被更改,并且尝试更改它可能会导致错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM