java线程学习19.3.22

关于线程开发的一些基础模型

/*
 * 协作模型:生产者消费者实现方式一:管程法
 * 借用缓冲区
 */
public class CoTest1 {
    public static void main(String[] args) {
        SynContainer container=new SynContainer();
        new Productor(container).start();
        new Consumer(container).start();
    }
}
//生产者
class Productor extends Thread{
    SynContainer container;
    
    public Productor(SynContainer container) {
        
        this.container = container;
    }

    public void run() {
        // 生产
        for(int i=0;i<100;i++) {
            System.out.println("生产-》"+i+"个馒头");
            container.push(new Steamedbun(i));
        }
    }
}
//消费者
class Consumer extends Thread{
    SynContainer container;
    
    public Consumer(SynContainer container) {
        
        this.container = container;
    }
    public void run() {
        // 消费
        for(int i=0;i<100;i++) {
            System.out.println("消费-》"+container.pop().id+"个馒头");
        }
    }
}
//缓冲区
class SynContainer extends Thread{
    Steamedbun[] buns=new Steamedbun[10];//存储容器
    int count=0;//计数器
    //存储  生产
    public synchronized void push(Steamedbun bun) {
        //何时生产 容器存在空间
        //不能生产
        if(count==buns.length) {
            try {
                this.wait();//线程阻塞 消费者通知生产
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        //存在空间  可以生产
        buns[count]=bun;
        count++;
        //生产了新数据  可以通知消费
        this.notifyAll();
    }
    //获取 消费
    public synchronized Steamedbun pop() {
        //何时消费  容器中是否存在数据
        //没有数据等待
        if(count==0) {
            try {
                this.wait();//线程阻塞 生产者通知消费则解除
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        //有数据消费
        count--;
        Steamedbun bun=buns[count];
        this.notifyAll();//消费了,存在空间,通知生产
        return bun;
    }
}
//馒头
class Steamedbun{
    int id;

    public Steamedbun(int id) {
        super();
        this.id = id;
    }
    
}

 

/*
 * 协作模型:生产者消费者实现方式二:信号灯法
 * 借助标志位
 */
public class CoTest2 {
    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 void run() {
        for(int i=0;i<20;i++) {
            if(i%2==0) {
                this.tv.play("奇葩说");
            }else {
                this.tv.play("广告");
            }
        }
    }
    public Player(TV tv) {
        super();
        this.tv = tv;
    }
}
//消费者 观众
class Watcher extends Thread{
    TV tv;
    
    public void run() {
        for(int i=0;i<20;i++) {
            tv.watch();
        }
    }
    public Watcher(TV tv) {
        super();
        this.tv = tv;
    }
}
//同一个资源 电视
class TV{
    String voice;
    //信号灯
    //T 标识演员表演 观众等待
    //F 表示观众观看 演员等待
    boolean flag=true;
    
    //表演
    public synchronized void play(String voice) {
        //演员等待
        if(!flag) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                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) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        System.out.println("听到了:"+voice);
        //唤醒
                this.notifyAll();
                this.flag=!this.flag;
    }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值