6、JUC:傳統的生產者消費者問題,防止虛假喚醒問題

@Eleven發表於2020-11-20

學習過程觀看視訊:[狂神說Java]
https://www.bilibili.com/video/BV1B7411L7tE?p=2
歡迎大家支援噢,很良心的老師了!

執行緒通訊案例:

package com.zjl;

/**
 * Created by zjl
 * 2020/11/20
 **/
/*
* 1、執行緒之間的通訊問題:生產者和消費者問題!
* 2、執行緒交替執行,A  B  操作同一個變數  num = 0
* 3、A  num+1
* 4、B  num-1
* */
public class Test {
    public static void main(String[] args) {
        Data data = new Data();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}


//等待-----》業務-----》通知
class Data{    //數字,資源類

    private int number = 0;

    //+1
    public synchronized void increment() throws InterruptedException {
        if(number!=0){
            //等待
            this.wait();
        }
        number++;
        System.out.println("我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一" + number);
        this.notifyAll();
    }

    //-1
    public synchronized void decrement() throws InterruptedException {
        if(number == 0){
            //等待
            this.wait();
        }
        number--;
        System.out.println("我減一完畢了--------------------------喚醒其他執行緒加一" + number);
        this.notifyAll();
    }

}

執行結果:

/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/bin/java "-javaagent:/Users/zhangjianlong/Library/Application Support/JetBrains/Toolbox/apps/IDEA-U/ch-0/193.5662.53/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=59773:/Users/zhangjianlong/Library/Application Support/JetBrains/Toolbox/apps/IDEA-U/ch-0/193.5662.53/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/tools.jar:/Users/zhangjianlong/testCode/JUC-study/target/classes:/Users/zhangjianlong/apache-maven-3.6.3/repository/org/projectlombok/lombok/1.18.8/lombok-1.18.8.jar com.zjl.Test
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一1
我減一完畢了--------------------------喚醒其他執行緒加一0
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一1
我減一完畢了--------------------------喚醒其他執行緒加一0
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一1
我減一完畢了--------------------------喚醒其他執行緒加一0
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一1
我減一完畢了--------------------------喚醒其他執行緒加一0
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一1
我減一完畢了--------------------------喚醒其他執行緒加一0
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一1
我減一完畢了--------------------------喚醒其他執行緒加一0
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一1
我減一完畢了--------------------------喚醒其他執行緒加一0
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一1
我減一完畢了--------------------------喚醒其他執行緒加一0
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一1
我減一完畢了--------------------------喚醒其他執行緒加一0
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一1
我減一完畢了--------------------------喚醒其他執行緒加一0

Process finished with exit code 0


當我們把執行緒增加到4個的時候,會出現問題:

package com.zjl;

/**
 * Created by zjl
 * 2020/11/20
 **/
/*
* 1、執行緒之間的通訊問題:生產者和消費者問題!
* 2、執行緒交替執行,A  B  操作同一個變數  num = 0
* 3、A  num+1
* 4、B  num-1
* */
public class Test {
    public static void main(String[] args) {
        Data data = new Data();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}


//等待-----》業務-----》通知
class Data{    //數字,資源類

    private int number = 0;

    //+1
    public synchronized void increment() throws InterruptedException {
        if(number!=0){
            //等待
            this.wait();
        }
        number++;
        System.out.println("我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一" + number);
        this.notifyAll();
    }

    //-1
    public synchronized void decrement() throws InterruptedException {
        if(number == 0){
            //等待
            this.wait();
        }
        number--;
        System.out.println("我減一完畢了--------------------------喚醒其他執行緒加一" + number);
        this.notifyAll();
    }

}

執行結果:

/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/bin/java "-javaagent:/Users/zhangjianlong/Library/Application Support/JetBrains/Toolbox/apps/IDEA-U/ch-0/193.5662.53/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=59846:/Users/zhangjianlong/Library/Application Support/JetBrains/Toolbox/apps/IDEA-U/ch-0/193.5662.53/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/tools.jar:/Users/zhangjianlong/testCode/JUC-study/target/classes:/Users/zhangjianlong/apache-maven-3.6.3/repository/org/projectlombok/lombok/1.18.8/lombok-1.18.8.jar com.zjl.Test
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一1
我減一完畢了--------------------------喚醒其他執行緒加一0
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一1
我減一完畢了--------------------------喚醒其他執行緒加一0
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一1
我減一完畢了--------------------------喚醒其他執行緒加一0
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一1
我減一完畢了--------------------------喚醒其他執行緒加一0
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一1
我減一完畢了--------------------------喚醒其他執行緒加一0
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一1
我減一完畢了--------------------------喚醒其他執行緒加一0
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一1
我減一完畢了--------------------------喚醒其他執行緒加一0
我減一完畢了--------------------------喚醒其他執行緒加一-1
我減一完畢了--------------------------喚醒其他執行緒加一-2
我減一完畢了--------------------------喚醒其他執行緒加一-3
我減一完畢了--------------------------喚醒其他執行緒加一-4
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一-3
我減一完畢了--------------------------喚醒其他執行緒加一-4
我減一完畢了--------------------------喚醒其他執行緒加一-5
我減一完畢了--------------------------喚醒其他執行緒加一-6
我減一完畢了--------------------------喚醒其他執行緒加一-7
我減一完畢了--------------------------喚醒其他執行緒加一-8
我減一完畢了--------------------------喚醒其他執行緒加一-9
我減一完畢了--------------------------喚醒其他執行緒加一-10
我減一完畢了--------------------------喚醒其他執行緒加一-11
我減一完畢了--------------------------喚醒其他執行緒加一-12
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一-11
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一-10
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一-9
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一-8
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一-7
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一-6
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一-5
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一-4
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一-3

在這裡插入圖片描述
比如說:當值為0時,2個加操作被喚醒了,同時執行如下程式碼,會造成加1加兩次

if(number!=0){
 //等待
    this.wait();
}
number++;

但是如果改成如下判斷,就只會執行一次加1

while(number!=0){
   //等待
    this.wait();
}
number++;

if 改為 while 判斷

package com.zjl;

/**
 * Created by zjl
 * 2020/11/20
 **/
/*
* 1、執行緒之間的通訊問題:生產者和消費者問題!
* 2、執行緒交替執行,A  B  操作同一個變數  num = 0
* 3、A  num+1
* 4、B  num-1
* */
public class Test {
    public static void main(String[] args) {
        Data data = new Data();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}


//等待-----》業務-----》通知
class Data{    //數字,資源類

    private int number = 0;

    //+1
    public synchronized void increment() throws InterruptedException {
        while (number!=0){
            //等待
            this.wait();
        }
        number++;
        System.out.println("我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一" + number);
        this.notifyAll();
    }

    //-1
    public synchronized void decrement() throws InterruptedException {
        while (number == 0){
            //等待
            this.wait();
        }
        number--;
        System.out.println("我減一完畢了--------------------------喚醒其他執行緒加一" + number);
        this.notifyAll();
    }

}

執行結果

/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/bin/java "-javaagent:/Users/zhangjianlong/Library/Application Support/JetBrains/Toolbox/apps/IDEA-U/ch-0/193.5662.53/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=59913:/Users/zhangjianlong/Library/Application Support/JetBrains/Toolbox/apps/IDEA-U/ch-0/193.5662.53/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/tools.jar:/Users/zhangjianlong/testCode/JUC-study/target/classes:/Users/zhangjianlong/apache-maven-3.6.3/repository/org/projectlombok/lombok/1.18.8/lombok-1.18.8.jar com.zjl.Test
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一1
我減一完畢了--------------------------喚醒其他執行緒加一0
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一1
我減一完畢了--------------------------喚醒其他執行緒加一0
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一1
我減一完畢了--------------------------喚醒其他執行緒加一0
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一1
我減一完畢了--------------------------喚醒其他執行緒加一0
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一1
我減一完畢了--------------------------喚醒其他執行緒加一0
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一1
我減一完畢了--------------------------喚醒其他執行緒加一0
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一1
我減一完畢了--------------------------喚醒其他執行緒加一0
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一1
我減一完畢了--------------------------喚醒其他執行緒加一0
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一1
我減一完畢了--------------------------喚醒其他執行緒加一0
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一1
我減一完畢了--------------------------喚醒其他執行緒加一0
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一1
我減一完畢了--------------------------喚醒其他執行緒加一0
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一1
我減一完畢了--------------------------喚醒其他執行緒加一0
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一1
我減一完畢了--------------------------喚醒其他執行緒加一0
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一1
我減一完畢了--------------------------喚醒其他執行緒加一0
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一1
我減一完畢了--------------------------喚醒其他執行緒加一0
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一1
我減一完畢了--------------------------喚醒其他執行緒加一0
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一1
我減一完畢了--------------------------喚醒其他執行緒加一0
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一1
我減一完畢了--------------------------喚醒其他執行緒加一0
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一1
我減一完畢了--------------------------喚醒其他執行緒加一0
我加一完畢了+++++++++++++++++++++++++喚醒其他執行緒減一1
我減一完畢了--------------------------喚醒其他執行緒加一0

Process finished with exit code 0

相關文章