This question already has an answer here:
这个问题在这里已有答案:
- static synchronized and non static synchronized methods in threads 3 answers
线程中的静态同步和非静态同步方法3答案
I am a bit confused here when we have to use class level locking for synchronization of static variables using synchronized(X.class)
.Here what I understand when a thread acquires a lock, no other thread will be able to access any other non-synchronized blocks as well which may incur a performance bottleneck. If there is a performance bottleneck what's the best way to deal with this problem.eg
当我们必须使用类级别锁定来使用synchronized(X.class)来同步静态变量时,我有点困惑。这是我在线程获取锁时所理解的,没有其他线程可以访问任何其他非线程同步块也可能导致性能瓶颈。如果存在性能瓶颈,那么处理这个问题的最佳方法是什么
class A {
static int A = 0;
public static synchorized func1() {
}
public synchorized func2() {
}
public synchorized func3() {
}
public synchorized func4() {
}
}
In this case if a thread accesses func1()
, no other thread will be able to access even other functions like func2()
,func3()
,func4()
-if I am correct.
在这种情况下,如果一个线程访问func1(),没有其他线程甚至可以访问其他函数,如func2(),func3(),func4() - 如果我是正确的。
1 个解决方案
#1
2
this is a question about class locking vs instance locking.
这是关于类锁定与实例锁定的问题。
public static synchronized test(){
}
public synchronized test2(){
}
These two methods are equivalent to:
这两种方法相当于:
public static test(){
synchronized(MyClass.class) {
}
}
public static test2(){
synchronized(this) {
}
}
They lock on different locks and thus do not block each other
.
它们锁定不同的锁,因此不会相互阻挡。
Your assumption that if a Thread calls func1
; no other Thread can call func2
and so-on is wrong, since they do use different locks.
你假设一个线程调用func1;没有其他线程可以调用func2而so-on是错误的,因为它们确实使用了不同的锁。
On the other hand (considering we are talking about the same instance being called upon) if ThreadA
calls func2
, then another ThreadB
can not call func3
until ThreadA
is done. This does not mean that it will for sure get the lock next - since there might be other threads waiting for that lock - and which one gets it is not guaranteed.
另一方面(考虑到我们正在讨论被调用的相同实例)如果ThreadA调用func2,则另一个ThreadB在ThreadA完成之前不能调用func3。这并不意味着它肯定会在下一次获得锁定 - 因为可能有其他线程在等待该锁定 - 并且哪一个获得它不能得到保证。
#1
2
this is a question about class locking vs instance locking.
这是关于类锁定与实例锁定的问题。
public static synchronized test(){
}
public synchronized test2(){
}
These two methods are equivalent to:
这两种方法相当于:
public static test(){
synchronized(MyClass.class) {
}
}
public static test2(){
synchronized(this) {
}
}
They lock on different locks and thus do not block each other
.
它们锁定不同的锁,因此不会相互阻挡。
Your assumption that if a Thread calls func1
; no other Thread can call func2
and so-on is wrong, since they do use different locks.
你假设一个线程调用func1;没有其他线程可以调用func2而so-on是错误的,因为它们确实使用了不同的锁。
On the other hand (considering we are talking about the same instance being called upon) if ThreadA
calls func2
, then another ThreadB
can not call func3
until ThreadA
is done. This does not mean that it will for sure get the lock next - since there might be other threads waiting for that lock - and which one gets it is not guaranteed.
另一方面(考虑到我们正在讨论被调用的相同实例)如果ThreadA调用func2,则另一个ThreadB在ThreadA完成之前不能调用func3。这并不意味着它肯定会在下一次获得锁定 - 因为可能有其他线程在等待该锁定 - 并且哪一个获得它不能得到保证。