线程通信之信号灯法

还是以生产者/消费者的问题为例,这次使用信号灯法来解决线程的通信问题。信号灯法个人感觉就是两个线程一替一个执行,下面看一下代码实现:

//生产者消费2
//生产者--->演员
//消费者--->观众
//产品:信号灯--->电视----->声音

public class TestPC2 {
     
    public static void main(String[] args) {
     
        TV tv = new TV();

        new Player(tv).start();
        new Watcher(tv).start();
    }
}


//生产者
class Player extends Thread{
     
    TV tv;

    public Player(TV tv){
     
        this.tv = tv;
    }
    @Override
    public void run() {
     
        for (int i = 0; i < 20; i++) {
     
            if (i%2==0){
     
                this.tv.play("节目:快乐大本营播放中");
                System.out.println();
            }else {
     
                this.tv.play("广告:抖音,记录美好生活");
            }
        }
    }
}

//消费者
class Watcher extends Thread{
     
    TV tv;
    public Watcher(TV tv){
     
        this.tv = tv;
    }
    @Override
    public void run() {
     
        for (int i = 0; i < 20; i++) {
     
            tv.watch();
        }
    }
}

//电视
class TV{
     
    
    boolean flag = true;  //  flag是信号灯,演员说话, 观众等待;观众观看 , 演员等待

    //说话
    String voice;

    //表演
    public synchronized void play(String voice){
     

        //演员等待
        if (!flag){
     
            try {
     
                this.wait();
            } catch (InterruptedException e) {
     
                e.printStackTrace();
            }
        }

        System.out.println("表演了"+voice);
        this.voice = voice;

        //让观众观看
        this.notifyAll();
        this.flag = !this.flag;

    }


    //观看
    public synchronized void watch(){
     


        //观众等待
        if (flag){
     
            try {
     
                this.wait();
            } catch (InterruptedException e) {
     
                e.printStackTrace();
            }
        }

        System.out.println("观众听到了: "+voice);

        //通知演员说话
        this.notifyAll();

        this.flag = !this.flag;
    }
}

上面的代码就是通过信号灯法来进行两个线程的通信,当生产者说一句话的时候,消费者听到一句话。

你可能感兴趣的:(信号灯法)