publicclass
radio
catch
(interruptedexception e)
system.out.println("classlock end, " +name);
}public
synchronized
void
instancelock()
catch
(interruptedexception e)
system.out.println(">>>>>instancelock end, " +name);
}public
static
void
main(string args) , "c1").start();
new thread(() ->, "c2").start();
new thread(() ->, "c3").start();
radio r = new
radio();
new thread(() ->, "i1").start();
new thread(() ->, "i2").start();
new thread(() ->, "i3").start();
}}
上圖的執行過程:
c1、c2、c3之間是互斥的。說明類鎖不管是物件呼叫(相同的物件還是不同的物件)還是類名呼叫,都是互斥的。
i1與i3是併發的, i1與i2是互斥的。說明物件鎖只作用於相同物件。
i1、i3與c1是併發的。說明物件鎖的獲取與類鎖的獲取是不影響的,可以併發。
物件鎖 與 類鎖
原文 一 什麼是物件鎖 物件鎖也叫方法鎖,是針對乙個物件例項的,它只在該物件的某個記憶體位置宣告乙個標識該物件是否擁有鎖,所有它只會鎖住當前的物件,而並不會對其他物件例項的鎖產生任何影響,不同物件訪問同乙個被synchronized修飾的方法的時候不會阻塞,例如 publicclassmyobjec...
物件鎖與類鎖
public class classlock public static void main string args for int i 0 i 3 i 輸出 thread 0 0thread 0 1thread 0 2thread 2 3thread 2 4thread 2 5thread 2 6...
synchronized物件鎖與類鎖
以前以為這兩個鎖是乙個鎖,最近測試了一下發現並不是這樣 如下 public class cusandpro public static synchronized void test2 catch interruptedexception e public static void main strin...