private final Object lockObject = new Object();
public void getCount() {
synchronized( lockObject ) {
...
}
}
为什么上面的代码比下面的代码更好:
public void synchronized getCount() {
...
}
我搜索并找到了如下所述的解释。
Putting it on the method means you are using the lock of the object itself to provide thread safety. With this kind of mechanism, it is possible for a malicious user of your code to also obtain the lock on your object, and hold it forever, effectively blocking other threads. A non-malicious user can effectively do the same thing inadvertently.
但我无法完全理解这一点。恶意用户如何能够永远持有锁?任何人都可以用示例代码进行解释,以证明上述情况的合理性吗?
最佳答案
与
public class Example {
public void synchronized getCount() {
...
}
}
它正在同步当前对象this
。其他类能够获取当前对象的引用并将其用作监视器锁:
public class OtherClass {
public void otherMethod() {
Example example = new Example();
synchronized (example) {
...
}
}
}
这可能会得到意想不到的结果,例如,导致 getCount
在执行 otherMethod
时被阻止。
使用第一种方法,由于监视器锁 lockObject
是私有(private)的,其他类无法直接访问它,因此优于第二种方法。
关于java - 私有(private)不可变对象(immutable对象)的同步块(synchronized block)和同步方法的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51007407/