生產者與消費者------信號燈法

之前寫過一篇生產者與消費者的管程法
管程法
對於一些思路可以先看一下這篇博客

什麼是信號燈法

我們知道,我們過馬路的時候是需要看信號燈的(可能有人不看)當綠燈亮的時候,行人可以走,紅燈亮的時候,行人不可以走,車子便可以走了,那麼同理,信號燈法就是這個意思,我們需要定一個變量來做信號燈,這裏推薦定義Boolean,那麼話不多說,我們直接上代碼,爲了方便大家理解,我們把對象寫成,年輕人,老人,年輕人去洗手間方便,方便出來後老人開始打掃衛生間。

代碼一覽(年輕人)

class youngMan implements Runnable
{
    WC wc;//定義一個洗手間類,後面會寫

    public youngMan(WC wc) {
        this.wc = wc;
    }

    @Override
    public void run() {
        for (int i = 1; i <= 20; i++) {
            if(i%2==0)//設計一個分歧條件
            {
                try {
                    this.wc.go_to_WC("大手");//把name傳入wc
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            else {
                try {
                    this.wc.go_to_WC("小手");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}

代碼一覽(老人)

class oldMan implements Runnable
{
    WC wc;

    public oldMan(WC wc) {
        this.wc = wc;
    }

    @Override
    public void run() {
        for(int i=1;i<=20;i++)
        {
            try {
                this.wc.clean_WC();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

代碼一覽(洗手間)

class WC
{
    boolean flag=true;
    String name;
    public synchronized void go_to_WC(String name) throws InterruptedException {
        if(!flag)
        {
            this.wait();
        }
        this.name=name;//把name傳入
        this.flag=!this.flag;
         System.out.println("男孩在衛生間--->"+name);
         this.notifyAll();
    }

    public synchronized void clean_WC() throws InterruptedException {
        if (flag)
        {
            this.wait();
        }

         System.out.println("老人在衛生間清理--->"+name);
          this.flag=!this.flag;
         this.notifyAll();
    }
}

代碼總覽

package Thread;

public class mytest02 {
    public static void main(String[] args) {
        WC wc =new WC();
        youngMan ymm=new youngMan(wc);
        oldMan omm=new oldMan(wc);
        Thread t1=new Thread(ymm);
        Thread t2=new Thread(omm);
        t1.start();
        t2.start();
    }
}
class oldMan implements Runnable
{
    WC wc;

    public oldMan(WC wc) {
        this.wc = wc;
    }

    @Override
    public void run() {
        for(int i=1;i<=10;i++)
        {
            try {
                this.wc.clean_WC();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class youngMan implements Runnable
{
    WC wc;

    public youngMan(WC wc) {
        this.wc = wc;
    }

    @Override
    public void run() {
        for (int i = 1; i <= 10; i++) {
            if(i%2==0)
            {
                try {
                    this.wc.go_to_WC("大手");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            else {
                try {
                    this.wc.go_to_WC("小手");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}

class WC
{
    boolean flag=true;
    String name;
    public synchronized void go_to_WC(String name) throws InterruptedException {
        if(!flag)
        {
            this.wait();
        }
        this.name=name;
        this.flag=!this.flag;
         System.out.println("男孩在衛生間--->"+name);
         this.notifyAll();
    }

    public synchronized void clean_WC() throws InterruptedException {
        if (flag)
        {
            this.wait();
        }

         System.out.println("老人在衛生間清理--->"+name);
          this.flag=!this.flag;
         this.notifyAll();
    }
}

結果

在這裏插入圖片描述

總結

總的來說不是很難理解,只要把向標立好,在加以理解便可,注意的是一定要鎖對地方,否則會有錯誤,如果有不能理解的,建議看一下我上一篇文章,鏈接在博客頂端,希望我寫的可以幫到你。

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