• 生产者与消费者以及多个容器的多线程问题(经典 集合了老师,同学以及自己的想法)


      1 package day2016_10_25_Thread;
      2 
      3 import java.util.LinkedList;
      4 import java.util.Random;
      5 
      6 public class Productor {
      7     public static void main(String[] args) throws InterruptedException {
      8 
      9         //多个容器
     10         Box box = new Box();
     11         Box box2 = new Box();
     12 
     13         //解决生产者,消费者,容器共用一个对象锁的问题
     14         Object obj=new Object();
     15         Producter p = new Producter(box, box2,obj);
     16         Customer c = new Customer(box, box2,obj);
     17 
     18         Thread tp = new Thread(p, "productor");
     19         Thread tc = new Thread(c, "consumer");
     20 
     21         tp.start();
     22         tc.start();
     23 
     24         Thread.sleep(3000);
     25         System.out.println(box.size() + "--------------------" + box2.size());
     26     }
     27 }
     28 
     29 class Producter implements Runnable {
     30     Object obj;
     31     Box box1;
     32     Box box2;
     33 
     34     Producter(Box box1, Box box2,Object obj) {
     35         this.box1 = box1;
     36         this.box2 = box2;
     37         this.obj=obj;
     38     }
     39 
     40     @Override
     41     public void run() {
     42 
     43         System.out.println(Thread.currentThread().getName());
     44 
     45         for (int i = 0; i < 30; i++) {
     46             synchronized (obj) {
     47                 obj.notify();  
     48                 if(i==49){
     49                     System.out.println("生产者30次循环结束!");
     50                 }
     51                 if (box1.size() >= 10 && box2.size() >= 10) {
     52                     try {
     53                         obj.wait(); // 本线程等待
     54                     } catch (InterruptedException e) {
     55                         e.printStackTrace();
     56                     }
     57                 } else if (box1.size() >= 10 && box2.size() < 10) {
     58                     box2.push(new Bread("bread-" + i));
     59                     System.out.println(Thread.currentThread().getName()
     60                             + "-> box2 : " + box2.size());
     61                 } else if (box1.size() < 10 && box2.size() >= 10) {
     62                     box1.push(new Bread("bread-" + i));
     63                     System.out.println(Thread.currentThread().getName()
     64                             + "-> box1 : " + box1.size());
     65                 } else {
     66                     Random ran = new Random();
     67                     int num = ran.nextInt(2);
     68                     if (num == 0) {
     69                         box1.push(new Bread("bread-" + i));
     70                         System.out.println(Thread.currentThread().getName()
     71                                 + "-> box1 : " + box1.size());
     72                     } else {
     73                         box2.push(new Bread("bread-" + i));
     74                         System.out.println(Thread.currentThread().getName()
     75                                 + "-> box2 : " + box2.size());
     76                     }
     77                 }
     78             }
     79         }
     80 
     81     }
     82 }
     83 
     84 class Customer implements Runnable {
     85     Object obj;
     86     Box box1;
     87     Box box2;
     88 
     89     Customer(Box box1, Box box2,Object obj) {
     90         this.box1 = box1;
     91         this.box2 = box2;
     92         this.obj=obj;
     93     }
     94 
     95     @Override
     96     public void run() {
     97 
     98         System.out.println(Thread.currentThread().getName());
     99 
    100         for (int i = 0; i < 30; i++) {
    101             synchronized (obj) {
    102                 obj.notify();
    103                 if(i==49){
    104                     System.out.println("消费者30次循环结束!");
    105                 }
    106                 if (box1.size() <= 3 && box2.size() <= 3) {
    107                     try {
    108                         obj.wait();
    109                     } catch (InterruptedException e) {
    110                         e.printStackTrace();
    111                     }
    112                 } else if (box1.size() <= 3 && box2.size() > 3) {
    113                     Bread b2 = box2.pop();
    114                     System.out.println(Thread.currentThread().getName()
    115                             + "-> box2 : " + b2);
    116                 } else if (box1.size() > 3 && box2.size() <= 3) {
    117                     Bread b1 = box1.pop();
    118                     System.out.println(Thread.currentThread().getName()
    119                             + "-> box1 : " + b1);
    120                 } else {
    121                     Random ran = new Random();
    122                     int num = ran.nextInt(2);
    123                     if (num == 0) {
    124                         Bread b1 = box1.pop();
    125                         System.out.println(Thread.currentThread().getName()
    126                                 + "-> box1 : " + b1);
    127                     } else {
    128                         Bread b2 = box2.pop();
    129                         System.out.println(Thread.currentThread().getName()
    130                                 + "-> box2 : " + b2);
    131                     }
    132 
    133                 }
    134             }
    135         }
    136     }
    137 }
    138 
    139 class Box {
    140     LinkedList<Bread> list = new LinkedList<Bread>();
    141     int maxNum = 20;
    142 
    143     public void push(Bread b) {
    144         if (list.size() < maxNum) {
    145             list.add(b);
    146         }
    147 
    148     }
    149 
    150     public Bread pop() {
    151         if (list.size() > 0) {
    152             return list.remove();
    153         }
    154         return null;
    155     }
    156 
    157     public int size() {
    158         return list.size();
    159     }
    160 }
    161 
    162 class Bread {
    163     private String sid;
    164 
    165     Bread(String sid) {
    166         this.sid = sid;
    167     }
    168 
    169     @Override
    170     public String toString() {
    171         return "sid= " + sid;
    172     }
    173 
    174 }
    Productor.java
  • 相关阅读:
    jQuery选择器
    有关ssh的理解
    前端WEB开发人员
    @antdesign/charts 解决echarts图宽高自适应问题,设置100%宽高显示异常
    npm run lintstaged:js报错
    node版本工具 nvm
    nginx域名隐性(地址栏域名不变)跳转
    mysql本地指定loginpath免密登录(mysql_config_editor的用法)
    Git管理查看自己是从那个分支建的分支(什么时间创建的)
    隐藏java代码中 连接数据库、redis等含密码信息方法【配置文件信息安全加密】
  • 原文地址:https://www.cnblogs.com/1020182600HENG/p/5998824.html
Copyright © 2020-2023  润新知