环形缓冲器Java实现

来源:互联网 发布:华中数控编程软件 编辑:程序博客网 时间:2024/06/10 19:10

在数据采取时,经常用缓冲器来暂时存放数据,显然,此时一定要有一个相互排斥机制以防止生产者和消费者进程同时对这个缓冲器中的同一个元素进行存取。同时,系统还要确保缓冲器已满时生产者进程不再试着往里添加信息,消费者进程在缓冲器为空时也不去取信息。

具体实现如下:

view plain
  1. package app;  
  2.   
  3.   
  4. public class CircularBuffer {  
  5.   
  6.       
  7.     int bufsize;  
  8.     SensorRecord[] store;  
  9.     int numberOfEntries = 0;  
  10.     int front = 0;  
  11.     int back = 0;  
  12.       
  13.     CircularBuffer(int n){  
  14.         bufsize = n;  
  15.         store =  new SensorRecord[bufsize];  
  16.           
  17.     }  
  18.       
  19.     /** 
  20.      * 存放数据 
  21.      * @param rec要存放的数据对象 
  22.      * @throws InterruptedException 
  23.      */  
  24.     synchronized void put(SensorRecord rec) throws InterruptedException{  
  25.         if(numberOfEntries == bufsize)  
  26.             wait();  
  27.         store[back] = new SensorRecord(rec.num, rec.degree);  
  28.         System.out.println("put " + rec.toString());  
  29.         back = back + 1;  
  30.         if(back ==  bufsize)  
  31.             back = 0;  
  32.         numberOfEntries += 1;  
  33.         notify();  
  34.     }  
  35.     /** 
  36.      * 取出数据 
  37.      * @return 
  38.      * @throws InterruptedException 
  39.      */  
  40.     synchronized SensorRecord get() throws InterruptedException{  
  41.         SensorRecord result = new SensorRecord(-1,-1);  
  42.         if0 == numberOfEntries )  
  43.             wait();  
  44.         result = store[front];  
  45.         System.out.println("get " + result.toString());  
  46.         front += 1;  
  47.         if(front == bufsize)  
  48.             front = 0;  
  49.         numberOfEntries -= 1;  
  50.         notify();  
  51.         return result;  
  52.               
  53.     }  
  54.   
  55. }  
  

完整代码如下(仅供学习参考):

view plain
  1. package app;  
  2.   
  3. public class BufferPool {  
  4.       
  5.     public static CircularBuffer  buf = new CircularBuffer(100);  
  6.   
  7. }  
  8.   
  9. package app;  
  10.   
  11. public class Get implements Runnable {  
  12.   
  13.   
  14.     public void run() {  
  15.         while (true) {  
  16.             try {  
  17.                 Thread.sleep(1000);  
  18.                 BufferPool.buf.get();  
  19.             } catch (InterruptedException e) {  
  20.                 // TODO Auto-generated catch block  
  21.                 e.printStackTrace();  
  22.             }  
  23.         }  
  24.     }  
  25.   
  26.   
  27. }  
  28.   
  29.   
  30. package app;  
  31.   
  32. public class Put implements Runnable {  
  33.       
  34.     public void run() {  
  35.         while (true) {  
  36.             int num = (int) (Math.random() * 1000);  
  37.             int degree = (int) (Math.random() * 1000);  
  38.             SensorRecord rec = new SensorRecord(num, degree);  
  39.             try {  
  40.                 Thread.sleep(10);  
  41.                 BufferPool.buf.put(rec);  
  42.             } catch (InterruptedException e) {  
  43.                 // TODO Auto-generated catch block  
  44.                 e.printStackTrace();  
  45.             }  
  46.         }  
  47.     }  
  48.   
  49. }  
  50.   
  51.   
  52. package app;  
  53.   
  54. public class SensorRecord {  
  55.       
  56.     public SensorRecord(int num2, int degree2) {  
  57.         // TODO Auto-generated constructor stub  
  58.         this.num = num2;  
  59.         this.degree = degree2;  
  60.     }  
  61.       
  62.     int num;  
  63.     int degree;  
  64.       
  65.     public String  toString(){  
  66.         return new String("num: " + num  + "; degree: " + degree);  
  67.     }  
  68.   
  69. }  
  70.   
  71.   
  72. package app;  
  73.   
  74. public class TestBuffer {  
  75.   
  76.     /** 
  77.      * @param args 
  78.      */  
  79.       
  80.       
  81.     public static void main(String[] args) {  
  82.         Get get = new Get();  
  83.         Put put = new Put();  
  84.         Thread thread = new Thread(get);  
  85.         Thread thread2 = new Thread(put);  
  86.         thread.start();  
  87.         thread2.start();  
  88.   
  89.     }  
  90. }