所謂的“線程八鎖”
其實就是看 synchronized 鎖住的是哪個對象
情況1:12 或 21都是有可能的,就看cpu先調度哪個線程
@Slf4j(topic = "c.Number")
class Number{
? ?public synchronized void a() {
? ? ? ?log.debug("1");
? }
? ?public synchronized void b() {
? ? ? ?log.debug("2");
? }
}
?
public static void main(String[] args) {
? ?Number n1 = new Number();
? ?new Thread(()->{ n1.a(); }).start();
? ?new Thread(()->{ n1.b(); }).start();
}
情況2:1s后12,或 2 1s后 1 ,還是看cpu先調度哪個線程
@Slf4j(topic = "c.Number")
class Number{
? ?public synchronized void a() {
? ? ? ?sleep(1); //睡眠1秒
? ? ? ?log.debug("1");
? }
? ?public synchronized void b() {
? ? ? ?log.debug("2");
? }
}
?
public static void main(String[] args) {
? ?Number n1 = new Number();
? ?new Thread(()->{ n1.a(); }).start();
? ?new Thread(()->{ n1.b(); }).start();
}
情況3:3 1s后 12 、 23 1s后 1 、 32 1s后 1,3肯定是最開始的打印的,就看1或2誰先打印
@Slf4j(topic = "c.Number")
class Number{
? ?public synchronized void a() {
? ? ? ?sleep(1);//睡眠1秒
? ? ? ?log.debug("1");
? }
? ?public synchronized void b() {
? ? ? ?log.debug("2");
? }
? ?public void c() { // 未加鎖
? ? ? ?log.debug("3");
? }
}
?
public static void main(String[] args) {
? ?Number n1 = new Number();
? ?new Thread(()->{ n1.a(); }).start();
? ?new Thread(()->{ n1.b(); }).start();
? ?new Thread(()->{ n1.c(); }).start();
}
情況4:2 1s 后 1,沒有互斥,同時運行,2先打印,sleep 1秒后打印1
@Slf4j(topic = "c.Number")
class Number{
? ?public synchronized void a() {
? ? ? ?sleep(1);//睡眠1秒
? ? ? ?log.debug("1");
? }
? ?public synchronized void b() {
? ? ? ?log.debug("2");
? }
}
?
public static void main(String[] args) {
? ?Number n1 = new Number();
? ?Number n2 = new Number();
? ?new Thread(()->{ n1.a(); }).start();
? ?new Thread(()->{ n2.b(); }).start();
}
情況5:2 1s 后 1,鎖住的對象不同,所以和題4一樣,不存在互斥。
@Slf4j(topic = "c.Number")
class Number{
? ?public static synchronized void a() {
? ? ? ?sleep(1);//睡眠1秒
? ? ? ?log.debug("1");
? }
? ?public synchronized void b() {
? ? ? ?log.debug("2");
? }
}
?
public static void main(String[] args) {
? ?Number n1 = new Number();
? ?new Thread(()->{ n1.a(); }).start();
? ?new Thread(()->{ n1.b(); }).start();
}
情況6:1s 后12, 或 2 1s后 1,還是看cpu先調度哪個線程
@Slf4j(topic = "c.Number")
class Number{
? ?public static synchronized void a() {
? ? ? ?sleep(1);//睡眠1秒
? ? ? ?log.debug("1");
? }
? ?public static synchronized void b() {
? ? ? ?log.debug("2");
? }
}
?
public static void main(String[] args) {
? ?Number n1 = new Number();
? ?new Thread(()->{ n1.a(); }).start();
? ?new Thread(()->{ n1.b(); }).start();
}
情況7:2 1s 后 1,鎖住的對象不同,所以和題4一樣,不存在互斥。
@Slf4j(topic = "c.Number")
class Number{
? ?public static synchronized void a() {
? ? ? ?sleep(1);//睡眠1秒
? ? ? ?log.debug("1");
? }
? ?public synchronized void b() {
? ? ? ?log.debug("2");
? }
}
?
public static void main(String[] args) {
? ?Number n1 = new Number();
? ?Number n2 = new Number();
? ?new Thread(()->{ n1.a(); }).start();
? ?new Thread(()->{ n2.b(); }).start();
}
情況8:1s 后12, 或 2 1s后 1,鎖著的同一個對象,還是看cpu先調度哪個線程
@Slf4j(topic = "c.Number")
class Number{
? ?public static synchronized void a() {
? ? ? ?sleep(1);//睡眠1秒
? ? ? ?log.debug("1");
? }
? ?public static synchronized void b() {
? ? ? ?log.debug("2");
? }
}
?
public static void main(String[] args) {
? ?Number n1 = new Number();
? ?Number n2 = new Number();
? ?new Thread(()->{ n1.a(); }).start();
? ?new Thread(()->{ n2.b(); }).start();
}