首页 > 其他分享 >练习——管程法,信号灯法简单的实现生产者消费者模型

练习——管程法,信号灯法简单的实现生产者消费者模型

时间:2023-04-29 20:00:45浏览次数:35  
标签:container product01 管程 练习 class 信号灯 void new public

package com.thread_;

//管程法解决生产者消费者模型
public class PC1 {
    public static void main(String[] args) {

        SynContainer container = new SynContainer();

        Productor productor = new Productor(container);
        Consumer consumer = new Consumer(container);
        productor.start();
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        consumer.start();

    }
}

//生产者
class Productor extends Thread{
    SynContainer container ;
    public Productor (SynContainer container){
        this.container = container;
    }
    //生产


    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("生产了第" + i + "个产品");
            container.pP(new Product(i));
        }
    }
}
//消费者
class Consumer extends Thread{
    SynContainer container ;
    public Consumer (SynContainer container){
        this.container = container;
    }

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("消费了第" + container.cC().id + "件产品");
        }
    }
}
//产品
class  Product {
    int id ;

    public Product(int id) {
        this.id = id;
    }
}
//缓存区
class SynContainer{

    //容器
    Product[] products = new Product[10];
    //计数器
    int count = 0;

    //生产者放入产品
    public synchronized void pP(Product product){
        //如果容器满,需要等待消费
        if (count == products.length){
            //通知消费者消费.生产等待
            System.out.println("等待消费!");
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //如果没满,放入产品
        products[count] = product;
        count++;
        //通知消费者消费
        this.notifyAll();
    }

    //消费者消费产品
    public synchronized Product cC(){
        //判断能否消费
        if (count == 0){
            //等待生产
            System.out.println("等待生产!");
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //消费
        count--;
        Product product = products[count];
        //消费结束,通知生产者生产
        this.notifyAll();
        return product;

    }

}

package com.thread_;

//信号灯法解决生产者消费者模型
public class PC2 {
    public static void main(String[] args) throws Exception{
        Product01 product01 = new Product01();
        new Productor01(product01).start();
        new Consumer01(product01).start();

    }
}

//生产者
class Productor01 extends Thread{
    Product01 product01 ;


    public Productor01(Product01 product01){
        this.product01 = product01;
    }

    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            if (i%2 == 0){
                this.product01.pP01("生产了一盒饼干");
            }else {
                this.product01.pP01("生产了一盒牛奶");
            }
        }
    }
}

//消费者
class Consumer01 extends  Thread{
    Product01 product01 ;
    public Consumer01(Product01 product01){
        this.product01 = product01;
    }

    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            product01.cC01();
        }
    }
}


//产品
class Product01 {
    //生产者生产,消费者等待   T
    //消费者消费,生产者等待   F
    String production;  //产品
    boolean flag = true;

    //生产
    public synchronized void pP01(String production){
        if (!flag){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("生产者生产了" + production);
        //通知消费者消费
        this.notifyAll();
        this.production = production;
        this.flag = !flag;
    }

    //消费
    public synchronized void cC01(){
        if (flag){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("消费者消费" + production);
        //通知生产者生产
        this.notifyAll();
        this.flag = !flag;

    }

}

标签:container,product01,管程,练习,class,信号灯,void,new,public
From: https://www.cnblogs.com/Q1u-ovo/p/17364420.html

相关文章

  • [练习记录] 《算法竞赛进阶指南》打卡活动
    89.a^b题目大意给\(a,b,p\)求\(a^b\modp\)。思路可以直接快速幂。当模数\(p\)为\(1\)的时候特判一下。代码lla,b,mod;llqpow(lla,llb){ llres=1; while(b){ if(b&1)res=res*a%mod; a=a*a%mod,b>>=1; } returnres;}in......
  • c++打卡练习(19)
    1.问题描述相传国际象棋是古印度舍罕王的宰相达依尔发明的。舍罕王十分喜爱象棋,决定让宰相自己选择何种赏赐。这位聪明的宰相指着8x8共64格的象棋棋盘说:陛下,请您赏给我一些麦子吧。就在棋盘的第1格中放1粒,第2格放2粒,第3格放4粒,以后每一格都比前一格增加一倍,依此放完棋盘上64格,我......
  • C语言--练习
    1、写一个函数输出a的二进制(补码)中1的个数。intcount_(inta){ intcount=0; for(inti=0;i<32;i++) { if(((a>>i)&1)==1) count++; } returncount;}intmain(){ intcount=0; inta=0; scanf("%d",&a); count=count_(a);......
  • java方法的内存及练习
    方法的内存一、方法调用的基本内存原理:Java内存分配栈:方法运行时使用的内存方法进栈运行,运行完毕就出栈堆:newl出来的,都在堆内存中开辟了一个小空间方法区:存储可以运行的class文件本地方法栈:JVM在使用操作系统功能的时候使用和我们开发无关寄存器:给CPU使用和......
  • 方法重载及练习
    方法的重载在同一个类中定义了多个同名的方法,这些同名的方法具有同种的功能。每个方法具有不同的参数类型或参数个数。这些同名的方法就构成了重载关系。总结:同一个类中方法名相同,参数不同的方法(形参不同)叫方法的重载,方法的重载与返回值无关参数不同:个数不同,类型不同,顺序不......
  • C++第四章课后练习题4-22
    1#include<iostream>2usingnamespacestd;3enumweekday{sunday,monday,tuesday,wednesday,thursday,friday,saturday4};5intmain()6{7inti;8weekdayd=thursday;9cout<<"d="<<d<<endl;10......
  • c++打卡练习(18)
    猜牌术魔术师利用一副牌中的13张黑桃,预先将它们排好后迭在一起,并使牌面朝下。然后他对观众说:我不看牌,只要数数就可以猜到每张牌是什么,我大声数数,你们听,不信?你们就看,魔术师将最上面的那张牌数为1,把它翻过来正好是黑桃A,他将黑桃A放在桌子上,然后按顺序从上到下数手中的余牌,第二次......
  • Java练习题(一)
    1、下列程序编译或者运行的结果是(D)    Publicstaticvoidmain(Stringargs[]){         Inta=10;         Intb,c;         If(a>50){             b=9;}c=b+a;System.out.println(c);}       ......
  • HTML5布局练习
    传统布局 HTML5布局新布局的意义 HTML5布局练习1结构层从上至下从左至右从外至里·······补充:li不利于搜索引擎收录,尽量少用2表现层熟练运用绝对定位排序:CSScomb插件:CSS属性排序工具。快捷键:ctrl+shift+C美化:ctrl+shift+H       ......
  • CKA练习
    创建一个名字为deployment-clusterrole且仅允许创建以下资源类型的新ClusterRole:DeploymentStatefulSetDaemonSetkubectlcreateclusterroledeployment-clusterrole--verb=create--resource=Deployments,StatefulSets,DaemonSets在现有的namespaceapp-team1中创......