线程间的通信

package ThreadTest.Day3;

/**
 * @author CCQ
 * 实现线程之间的通信
 * 管程法
 *  void notify()
 *  唤醒在此对象监视器上等待的单个线程。
 *  void notifyAll()
 *  唤醒在此对象监视器上等待的所有线程。
 *  void wait()
 *  在其他线程调用此对象的notify()方法或 notifyAll()方法前,导致当前线程等待。
 *  void wait(long timeout)
 *  在其他线程调用此对象的 notify() 方法或 notifyAll() 方法,或者超过指定的时间量前,导致当前线程等待。
 */
public class Test06 {
    public static void main(String[] args) {
        Container container =new Container();

        new Thread(new product(container)).start();

        new Thread(new consumer(container)).start();
    }
}

//产品
class Chicken{
    int id;

    public Chicken(int id) {
        this.id = id;
    }
}

//容器
class Container{
    Chicken [] chickens =new Chicken[10];
    int count =0;//容器计数器
    //生产者放入产品
    public synchronized void push(Chicken chicken) throws InterruptedException {
        //如果容器满了就等待消费者消费
        if (count ==chickens.length){
            //通知消费者消费,停止生产
            this.wait();
        }

        //如果容器没有满就放入产品
        chickens[count] =chicken;
        count++;

        //通知生产者消费
        this.notifyAll();
    }

    //消费者消费产品
    public synchronized void pop() throws InterruptedException {
        //如果鸡没有了
        if (count ==0){
            //通知生产者生产,消费者等待
            this.wait();
        }
        //如果可以消费
        count--;
        System.out.println("消费了第"+chickens[count].id+"只鸡");
        //吃完了,通知生产者生产
        this.notifyAll();
    }
}

//生产者
class product implements Runnable{
    Container container;

    public product(Container container) {
        this.container = container;
    }

    @Override
    public void run() {
        for (int i = 1; i <= 100; i++) {
            //生产
            try {
                container.push(new Chicken(i));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("生产了第"+i+"只鸡");
        }
    }
}

//消费者
class consumer implements Runnable{
    Container container;

    public consumer(Container container) {
        this.container = container;
    }

    @Override
    public void run() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for (int i = 1; i <= 100; i++) {
            //消费
            try {
                    container.pop();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }
}
package ThreadTest.Day3;

/**
 * @author CCQ
 * 信号灯法:通过标志位帮助线程通信
 */
public class Test07 {
    public static void main(String[] args) {
        Tv tv =new Tv();
        new Thread(new player(tv)).start();
        new Thread(new watcher(tv)).start();
    }
}

class Tv{
    boolean flag =true;//T,演员表演,观众等待。F,观众观看,演员等待


    String name;//表演的节目名字

    //表演
    public synchronized void play(String name) throws InterruptedException {

        //如果观众正在看,演员等待
        if (!flag){
            this.wait();
        }

        //演员表演
        System.out.println("演员表演了:"+name);
        this.name =name;
        //通知观众去看
        this.notifyAll();
        this.flag =!this.flag;
    }

    //观看
    public synchronized void watch() throws InterruptedException {
        //如果演员正在表演,观众等待
        if (flag){
            this.wait();
        }

        //观众观看
        System.out.println("观众观看了:"+name);

        //通知演员表演
        this.flag =!this.flag;
        this.notifyAll();


    }
}

//演员
class player implements Runnable{
    Tv tv;

    public player(Tv tv) {
        this.tv = tv;
    }

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            if (i%2==0) {
                try {
                    this.tv.play("快乐大本营");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            else {
                try {
                    this.tv.play("抖音,记录美好生活");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
//观众
class watcher implements Runnable{

    Tv tv;

    public watcher(Tv tv) {
        this.tv = tv;
    }

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            try {
                tv.watch();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值