java 同步(this)和同步方法有什么区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4394976/
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
What is the difference between synchronized(this) and synchronized method
提问by Tarik
Lets say we have these 2 sample code :
假设我们有这两个示例代码:
public synchronized void getSomething(){
this.hello = "hello World";
}
and this one
还有这个
public void getSomething(){
synchronized(this){
this.hello = "hello World";
}
}
So some one can tell me what's the difference now?
所以有人可以告诉我现在有什么区别吗?
回答by Mike Clark
The two different methods are functionally equivalent. There maybe a very small performancedifference:
这两种不同的方法在功能上是等效的。有可能是一个非常小的性能差异:
At the bytecode level, the synchronized methodadvertises its need for synchronization as a bit set in the method's access flag. The JVM looks for this bit flag and synchronizes appropriately.
在字节码级别,同步方法将其同步需求作为方法访问标志中设置的位进行通告。JVM 查找此位标志并进行适当的同步。
The synchronized blockimplements its synchronization through a sequence of bytecode operations stored in the class file's definition of the method.
该同步块通过存储在该方法的类文件中的字节码定义的操作序列实现其同步。
So the synchronized method mightpotentially execute slightly faster and take up less space in terms of bytecode.
因此,就字节码而言,同步方法可能执行得稍微快一些,占用的空间更少。
Again, the two are, by specification, functionally identical.
同样,根据规范,两者在功能上是相同的。
I'm guessing that the performance difference is negligible and code style guidelines should win out. Some compilers might even optimize away the block into an access flag. And JIT may take the performance difference away.
我猜测性能差异可以忽略不计,代码风格指南应该会胜出。一些编译器甚至可能将块优化为访问标志。而 JIT 可能会消除性能差异。
回答by Carrotman42
Check out this portion of this article:
查看本文的这一部分:
http://www.ibm.com/developerworks/ibm/library/it-haggar_bytecode/#4
http://www.ibm.com/developerworks/ibm/library/it-haggar_bytecode/#4
It explains that while functionally congruent (synchronizing a method either locks on the instance Object or in the case of a static method the Class object of the class in which the method resides), synchronizing a method is much more optimal because rather than synchronizing in bytecode (as the synchronized block statements do), it synchronizes at the JVM level.
它解释说,虽然在功能上是一致的(同步方法要么锁定实例对象,要么在静态方法的情况下锁定方法所在类的类对象),同步方法更优化,因为而不是在字节码中同步(正如同步块语句所做的那样),它在 JVM 级别进行同步。
回答by D.C.
One difference is the granularity of the code that is synchronized. In the first example you are essentially locking the entire method, while in the second example only a section of the method will be locked. The second approach is better for long methods whose bodies do not need to be completely synchronized. Its best to only lock when you need to and release that lock for other threads as soon as possible.
一个区别是同步代码的粒度。在第一个示例中,您实际上锁定了整个方法,而在第二个示例中,仅锁定了方法的一部分。对于不需要完全同步主体的长方法,第二种方法更好。最好只在需要时锁定,并尽快为其他线程释放该锁定。