多线程06:生产者与消费者线程同步

一、生产球和消费球代码演示

  • 创建一个Ball类
//球产品
public class Ball {

  //球的个数
   int id;
   public Ball(int id) {
            this.id = id;
    }
}
  • 创建一个人容器缓存区SynContainer类
//缓冲区
public class SynContainer{

    //容器大小
    Ball[] balls=new Ball[2];

    //容器计数器
    int count=0;

    //生产者放入产品
    public synchronized void push(Ball ball){
        //如果容器满了,就需要等待消费者消费
        if (count==balls.length){
            //通知消费者消费,生产者等待
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //如果容器没有满,生产者将产品丢入到容器
        balls[count]=ball;
        count++;
        //可以通知消费过来者消费了
        this.notifyAll();
    }

    //消费者消费产品
    public synchronized Ball pop(){
        //判断是否可以消费
        if (count==0){
            //等待生产者生产,消费者等待
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
        //如果可以消费
        count--;
        Ball ball= balls[count];

        //通知生产者生产产品,放到缓冲区
        this.notifyAll();
        return ball;
    }
}
  • 创建生产者类继承Thread类
//生产者
public class Producer extends Thread{

    private SynContainer synContainer;

    public Producer(SynContainer synContainer){
        this.synContainer=synContainer;
    }

    //生产者生产产品
    @Override
    public void run() {
        for (int i = 1; i < 10; i++) {
            synContainer.push(new Ball(i));
            System.out.println("生产了第"+i+"个球");
        }
    }
}
  • 创建消费者类继承Thread类
//消费者
public class Consumer extends Thread{

   private SynContainer synContainer;

    public Consumer(SynContainer synContainer){
        this.synContainer=synContainer;
    }

    //消费者消费产品
    @Override
    public void run() {
        for (int i = 1; i < 10; i++) {
            System.out.println("消费了第--->"+synContainer.pop().id+"个球");
        }
    }
}
  • 测试
//测试生产者和消费者,利用缓冲区解决 :管程法
public class BallTest {
    public static void main(String[] args) {
        SynContainer synContainer = new SynContainer();

        new Producer(synContainer).start();
        new Consumer(synContainer).start();
    }
}
  • 运行结果
    在这里插入图片描述

二、演员和观众代码演示

  • 创建一个Tv节目类
//产品-->节目
public class Tv{
    //表演的节目
    String game;
    //标志位
    boolean flag=true;
    //表演
    public synchronized void paly(String game){
        if (!flag){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("演员表演了"+game);
        //通知观众观看
        this.notifyAll();
        this.game=game;
        this.flag=!this.flag;

    }

    //观看
    public synchronized void watch(){
        if (flag){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("观众观看了==================>"+game);
        //通知演员表演
        this.notifyAll();;
        this.flag=!this.flag;
    }
}
  • 创建一个生产者演员类继承Thread类
//生产者-->演员
public class Player extends Thread{
    private Tv tv;
    public Player(Tv tv){
        this.tv=tv;
    }

    @Override
    public void run() {
        for (int i = 0; i <5; i++) {
            if (i%2==0){
                this.tv.paly("快乐向前冲 ");
            }else {
                this.tv.paly("一家老小向前冲 ");
            }
        }
    }
}
  • 创建一个消费者观众类继承Thread类
//消费者-->观众
public class Watcher extends Thread{
    private Tv tv;
    public Watcher(Tv tv){
        this.tv=tv;
    }

    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            tv.watch();
        }
    }
}
  • 测试
//测试生产者消费者:信号灯法
public class ShowTest {
    public static void main(String[] args) {
        Tv tv = new Tv();
        new Player(tv).start();
        new Watcher(tv).start();
    }
}
  • 运行结果
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

微笑AJJD

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值