您的位置:首页 > 其它

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

2016-10-25 23:58 513 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: