Java併發(十六)----線程八鎖

所謂的“線程八鎖”

其實就是看 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();
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章