java线程之生产者消费者

看了毕向东老师的生产者消费者,就照着视频参考运行了一下,感觉还好

这个值得学习的是条理特别清晰:

ProducterConsumerDemo.java中,一个资源类Resources,生产者消费者都可以访问的到。

生产者类Producter,消费者Consumer都实现了Runnable接口,在其中的run方法中实现重载,对共享资源进行生产和消费

优化:

如果以后需要加入项目中,对ProducterConsumerDemo类中加一个构造方法,public ProducterConsumerDemo(){...},实例化对象即可调用

代码:

 1 class  ProducterConsumerDemo
 2 {
 3     public static void main(String[] args) 
 4     {
 5         Resources  r =new Resources();
 6         Productor  pro =new Productor(r);
 7         Consumer   con = new Consumer(r);
 8         
 9         Thread t1 =new Thread(pro);
10         Thread t2 =new Thread(con);
11         t1.start();
12         t2.start();
13         System.out.println("Hello World!");
14     }
15 }
16 
17 class Resources
18 {
19     private String name;
20     private int count =1;
21     private boolean flag =false;
22 
23     public synchronized void set(String name)
24     {
25        if(flag)
26            try{this.wait();}catch(Exception e){}
27        this.name = name+"--"+count++;
28 
29        System.out.println(Thread.currentThread().getName()+"生产者"+this.name);
30        flag =true;
31        //唤醒对方进程
32        this.notify();
33 
34     }
35     public synchronized void out()
36     {
37        if(!flag)
38            try{this.wait();}catch(Exception e){}
39        
40        System.out.println(Thread.currentThread().getName()+" ....消费者...."+this.name);
41        flag =false;
42        //唤醒对方进程
43        this.notify();
44 
45     }
46 }
47 
48 class Productor implements Runnable
49 {
50     private Resources res;
51     Productor(Resources res){
52         this.res =res;
53     }
54     public void run(){
55         while(true){
56             res.set("++商品++");
57         }
58     }
59 
60 }
61 
62 class Consumer  implements Runnable
63 {
64     private Resources res;
65     Consumer(Resources res){
66         this.res =res;
67     }
68     public void run(){
69         while(true){
70             res.out();
71         }
72     }
73 
74 }

由于自己需要的功能是,生产者消费者对一个二维数组进行操作,所以在其基础上加了资源Resources类的成员属性。实现存取功能

算是一个框架吧,留存:

 1 class  ProducterConsumerArr2D
 2 {
 3     public static void main(String[] args) 
 4     {
 5         Resources  r =new Resources();
 6         Productor  pro =new Productor(r);
 7         Consumer   con = new Consumer(r);
 8         
 9         Thread t1 =new Thread(pro);
10         Thread t2 =new Thread(con);
11         t1.start();
12         t2.start();
13         System.out.println("Hello World!");
14     }
15 }
16 
17 class Resources
18 {
19     private String name;
20     private int count =1;
21     private float[][] arr2D=new float[5][5];       //为它分配5行5列的空间大小
22     private boolean flag =false;
23 
24     public synchronized void set(String name)
25     {
26        if(flag)
27            try{this.wait();}catch(Exception e){}
28        //需放在count++前面
29         for(int i=0;i<5;i++)
30            for(int j=0;j<5;j++)
31         {
32            arr2D[i][j]=(float)count;
33         }
34        this.name = name+"--"+count++;
35        System.out.println(Thread.currentThread().getName()+"生产者"+this.name);
36       
37        flag =true;
38        //唤醒对方进程
39        this.notify();
40 
41     }
42     public synchronized void out()
43     {
44        if(!flag)
45            try{this.wait();}catch(Exception e){}
46        
47        System.out.println(Thread.currentThread().getName()+" ....消费者...."+this.name+"二维数组任意元素"+arr2D[2][2]);
48        flag =false;
49        //唤醒对方进程
50        this.notify();
51 
52     }
53 }
54 
55 class Productor implements Runnable
56 {
57     private Resources res;
58     Productor(Resources res){
59         this.res =res;
60     }
61     public void run(){
62         while(true){
63             res.set("++二维数组存取++");
64         }
65     }
66 
67 }
68 
69 class Consumer  implements Runnable
70 {
71     private Resources res;
72     Consumer(Resources res){
73         this.res =res;
74     }
75     public void run(){
76         while(true){
77             res.out();
78         }
79     }
80 
81 }
ProducterConsumerArr2D.java

当然,这个只针对支持两个互相独立的线程,如果继续加入多个线程(>2)肯定还会有资源数据出错的问题,继续学习

原文地址:https://www.cnblogs.com/shuqingstudy/p/5056672.html