根據synchronized的位置分爲同步方法和同步快:
同步方法形如:
public synchronized void getValue(){
...
}
public synchronized void getValue(){
...
}
該處鎖的對象是這個對象.
同步快形如:
public void getValue(){
synchronized (obj){
...
}
}
該處鎖的對象是obj。
還有兩外一種同步方法:
public synchronized static void getValue(){
...
}
該處鎖的對象是class
鎖可以實現對象、方法快的互斥訪問。也會提高死鎖的可能。