生产者与消费者-N:N-基于list

2018-09-01 05:42:31来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

多个生产者/多个消费者:

  

 1 /**
 2  *    生产者
 3  */
 4 public class P {
 5     private MyStack stack;
 6     
 7     public P(MyStack stack) {
 8         this.stack = stack;
 9     }
10     
11     public void pushService() {
12         stack.push();
13     }
14 }
 1 /**
 2  *    消费者
 3  */
 4 public class C {
 5     
 6     private MyStack stack;
 7     
 8     public C(MyStack stack) {
 9         this.stack = stack;
10     }
11     
12     public void popService() {
13         System.out.println("pop = " + stack.pop());
14     }
15 }
 1 import java.util.ArrayList;
 2 import java.util.List;
 3 
 4 /**
 5  *    模拟栈
 6  */
 7 public class MyStack {
 8     private List<Object> list = new ArrayList<>();
 9     
10     public synchronized void push() {
11         try {
12             while(list.size() == 1) {
13                 this.wait();
14             }
15             list.add(""+Math.random());
16             this.notifyAll();
17             System.out.println("push:" + list.size());
18         } catch (InterruptedException e) {
19             e.printStackTrace();
20         }
21     }
22     
23     
24     public synchronized String pop() {
25         String value = "";
26         try {
27             while(list.size() == 0) {
28                 System.out.println("pop 操作中:" + Thread.currentThread().getName() + "wait状态");
29                 this.wait();
30             }
31             value = ""+list.get(0);
32             list.remove(0);
33             this.notifyAll();
34             System.out.println("pop:" + list.size());
35         } catch (InterruptedException e) {
36             e.printStackTrace();
37         }
38         return value;
39     }
40 }
 1 /**
 2  *    生产者线程
 3  */
 4 public class P_Thread extends Thread {
 5     private P p;
 6     
 7     public P_Thread(P p) {
 8         this.p = p;
 9     }
10     
11     @Override
12     public void run() {
13         while (true) {
14             p.pushService();
15         }
16     }
17 }
 1 /**
 2  *    消费者线程
 3  */
 4 public class C_Thread extends Thread {
 5     
 6     private C c;
 7     
 8     public C_Thread(C c) {
 9         this.c = c;
10     }
11     
12     @Override
13     public void run() {
14         while (true) {
15             c.popService();
16         }
17     }
18 }
 1 /**
 2  *    测试类
 3  */
 4 public class Run {
 5     public static void main(String[] args) {
 6         MyStack stack = new MyStack();
 7         //三个生产者
 8         P p1 = new P(stack);
 9         P p2 = new P(stack);
10         P p3 = new P(stack);
11         //三个消费者
12         C c1 = new C(stack);
13         C c2 = new C(stack);
14         C c3 = new C(stack);
15         
16         //三个生产者线程
17         P_Thread pThread1 = new P_Thread(p1);
18         P_Thread pThread2 = new P_Thread(p2);
19         P_Thread pThread3 = new P_Thread(p3);
20         
21         //三个消费者线程
22         C_Thread cThread1 = new C_Thread(c1);
23         C_Thread cThread2 = new C_Thread(c2);
24         C_Thread cThread3 = new C_Thread(c3);
25         
26         //启动
27         pThread1.start();
28         pThread2.start();
29         pThread3.start();
30         cThread1.start();
31         cThread2.start();
32         cThread3.start();
33     }
34 }

运行结果:

  

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Javaweb文件上传的前端和后端

下一篇:将内网ip映射到外网域名