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 }优质内容筛选与推荐>>