java消费者模式


 1 public class Producer implements Runnable 
 2 { 
 3     GeoPointStack geoPointStack = new GeoPointStack(); 
 4     GeoPoint geoPoint;
 5  
 6     public Producer(GeoPointStack geoPointStack, GeoPoint geoPoint) {
 7         super();
 8         this.geoPointStack = geoPointStack;
 9         this.geoPoint = geoPoint;
10     }
11 
12     /** 
13     * show 生产进程. 
14     */  
15     public void run(){ 
16          geoPointStack.push(geoPoint); 
17     } 
18 }
 1 public class Consumer implements Runnable {
 2 
 3     GeoPointStack geoPointStack = new GeoPointStack();
 4     UploadPoint uploadPoint = new UploadPoint();
 5 
 6     Consumer(GeoPointStack geoPointStack) {
 7         this.geoPointStack = geoPointStack;
 8     }
 9 
10     Boolean runing = true;
11     String paramsStr = "";
12 
13     /**
14      * show 消费进程.
15      */
16     public void run() {
17         while (runing) {
18             try {
19                 GeoPoint geoPoint = geoPointStack.pop();25                 // 存的数据已经取完
26                 if (geoPointStack.over()) {29                 }
30             } catch (Exception e) {
31                 e.printStackTrace();
32             }
33         }
34     }
35 
36     public void stop() {
37         runing = false;
38     }
39 
40 }
 1 public class GeoPointStack {
 2     GeoPoint sm[] = new GeoPoint[10];
 3     int index = 0;
 4     Trace trace = new Trace();
 5     public synchronized void push(GeoPoint m) {
 6         try {
 7             while (index == sm.length) {
 8                 System.out.println("!!!!!!!!!生产满了!!!!!!!!!");
 9                 this.notify();
10                 this.wait();
11             }
12         } catch (InterruptedException e) {
13             e.printStackTrace();
14         } catch (IllegalMonitorStateException e) {
15             e.printStackTrace();
16         }
17 
18         sm[index] = m;
19         index++;
20         trace.add(m);
21         System.out.println("生产了:" + m + " 共" + index + "个点");
22     }
23 
24     public synchronized GeoPoint pop() {
25         try {
26             while (index == 0) {
27                 System.out.println("!!!!!!!!!上传光了!!!!!!!!!");
28                 this.notify();
29                 this.wait();
30             }
31         } catch (InterruptedException e) {
32             e.printStackTrace();
33         } catch (IllegalMonitorStateException e) {
34             e.printStackTrace();
35         }
36         index--;
37         System.out.println("上传了:---------" + sm[index] + " 共" + index + "个点");
38         return sm[index];
39     }
40     public Boolean over() {
41         return index==0;
42     }
43 }

优质内容筛选与推荐>>
1、Volley框架的使用
2、20170711xlVBA自定义分类汇总一例
3、CSS总结:
4、CSS实现下拉菜单的几种方法
5、Spring Cloud for Alibaba 之 简单集成 Sentinel (4)


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn