Java synchronized method lock on object, or method?
Posted
by wuntee
on Stack Overflow
See other posts from Stack Overflow
or by wuntee
Published on 2010-06-15T17:38:08Z
Indexed on
2010/06/15
17:42 UTC
Read the original article
Hit count: 302
If I have 2 synchronized methods in the same class, but each accessing different variables, can 2 threads access those 2 methods at the same time? Does the lock occur on the object, or does it get as specific as the variables inside the synchronized method?
Example:
class x{
private int a;
private int b;
public synchronized void addA(){
a++;
}
public synchronized void addB(){
b++;
}
}
Can 2 threads access the same instance of class x performing x.addA() and x.addB() at the same time?
© Stack Overflow or respective owner