[Java] 生产者与消费者 →→→→→进入此内容的聊天室

来自 , 2021-02-17, 写在 Java, 查看 135 次.
URL http://www.code666.cn/view/186a157b
  1. public class Goods {//商品类
  2.         //私有属性
  3.         private String brand;//品牌
  4.         private String name;//商品的名称
  5.         private boolean flag;//标识是否有商品,默认值是false,代表没有商品,
  6.         //公有的取值,赋值方法
  7.         public String getBrand() {
  8.                 return brand;
  9.         }
  10.         public void setBrand(String brand) {
  11.                 this.brand = brand;
  12.         }
  13.         public String getName() {
  14.                 return name;
  15.         }
  16.         public void setName(String name) {
  17.                 this.name = name;
  18.         }
  19.         //生产商品的方法
  20.         public synchronized void produc(String brand,String name){
  21.                 if (flag) {//flag的值为false时,没有商品,生产者生产商品,如果flag的值为true时,代表有商品,生产者等待
  22.                         try {
  23.                                 super.wait();
  24.                         } catch (InterruptedException e) {
  25.                                 // TODO Auto-generated catch block
  26.                                 e.printStackTrace();
  27.                         }//调用的是Object类中的wait方法
  28.                 }//被唤醒后,代表没有商品了,没有商品就开始生产
  29.                 this.brand=brand;//局部变量的值赋值给成员变量
  30.                 try {
  31.                         Thread.sleep(300);
  32.                 } catch (InterruptedException e) {
  33.                         // TODO Auto-generated catch block
  34.                         e.printStackTrace();
  35.                 }
  36.                 this.name=name;
  37.                 System.out.println("生产者生产了"+this.brand+"----------------"+this.name);
  38.                 /**商品生产结束*/
  39.                 flag=true;
  40.                 super.notify();//通知消费者线程
  41.                
  42.         }
  43.         //消费者取走商品的同步方法
  44.         public synchronized void custom(){
  45.                 if (flag==false) {//没有商品时,消费者等待,
  46.                         try {
  47.                                 super.wait();
  48.                         } catch (InterruptedException e) {
  49.                                 // TODO Auto-generated catch block
  50.                                 e.printStackTrace();
  51.                         }//没有商品时,消费者等待
  52.                 }//有商品时,取走商品
  53.                 try {
  54.                         Thread.sleep(300);
  55.                 } catch (InterruptedException e) {
  56.                         // TODO Auto-generated catch block
  57.                         e.printStackTrace();
  58.                 }
  59.                 System.out.println("--------------------消费者取走了商品-----------"+this.brand+"==========="+this.name);
  60.                 /**商品已取走*/
  61.                 flag=false;
  62.                 super.notify();
  63.         }
  64.        
  65. }
  66.  
  67. //生产者线程
  68. public class Producer implements Runnable {
  69.         //共享资源作为类的属性有属性
  70.         private Goods g;
  71.         //带参构造方法
  72.         public Producer(Goods g) {
  73.                 super();
  74.                 this.g = g;
  75.         }
  76.  
  77.         @Override
  78.         public void run() {//生产者要做的事情是生产商品
  79.                 for (int i = 0; i <10; i++) {
  80.                         if (i%2==0) {//偶数
  81.                                 //调用生产商品的方法
  82.                                 g.produc("娃哈哈", "矿泉水");
  83.                         }else {//奇数
  84.                                 g.produc("旺仔", "小馒头");
  85.                         }
  86.                        
  87.                 }
  88.         }
  89.  
  90. }
  91. package cn.bjsxt.pc3;
  92. //生产者线程
  93. public class Producer implements Runnable {
  94.         //共享资源作为类的属性有属性
  95.         private Goods g;
  96.         //带参构造方法
  97.         public Producer(Goods g) {
  98.                 super();
  99.                 this.g = g;
  100.         }
  101.  
  102.         @Override
  103.         public void run() {//生产者要做的事情是生产商品
  104.                 for (int i = 0; i <10; i++) {
  105.                         if (i%2==0) {//偶数
  106.                                 //调用生产商品的方法
  107.                                 g.produc("娃哈哈", "矿泉水");
  108.                         }else {//奇数
  109.                                 g.produc("旺仔", "小馒头");
  110.                         }
  111.                        
  112.                 }
  113.         }
  114.  
  115. }
  116.  
  117. //消费者的线程-->负责取商品
  118. public class Customer implements Runnable {
  119.         //共享资源是商品
  120.         private Goods gg;//共享资源作为线程类的私有属性
  121.         //带参构造方法
  122.         public Customer(Goods gg) {
  123.                 super();
  124.                 this.gg = gg;
  125.         }
  126.         @Override
  127.         public void run() {//这是消费者线程,负责取走商品
  128.                 for (int i = 0; i <10; i++) {
  129.                         //调用取走的同步方法
  130.                         gg.custom();
  131.                 }
  132.         }
  133.        
  134.        
  135. }
  136.  
  137.  
  138. public class Test {
  139.         public static void main(String[] args) {
  140.                 /**1.创建共享资源的对象*/
  141.                 Goods g=new Goods();
  142.                 /**2.创建生产者线程的对象*/
  143.                 Producer p=new Producer(g);
  144.                 /**3.创建消费者线程的对象*/
  145.                 Customer c=new Customer(g);
  146.                 //启动两个线程
  147.                 new Thread(p).start();//启动生产者线程
  148.                 new Thread(c).start();//启动消费者线程
  149.         }
  150. }
  151.  

回复 "生产者与消费者"

这儿你可以回复上面这条便签

captcha