免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 823 | 回复: 0
打印 上一主题 下一主题

环形缓冲器Java实现 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-23 03:35 |只看该作者 |倒序浏览
在数据采取时,经常用户缓冲器来暂时存放数据,显然,此时一定要有一个相互排斥机制以防止生产者和消费者进程同时对这个缓冲器中的同一个元素进行存取。同时,系统还要确保缓冲器已满时生产者进程不再试着往里添加信息,消费者进程在缓冲器为空时也不去取信息。

具体实现如下:

view plaincopy to clipboardprint?
package app;   
  
  
public class CircularBuffer {   
  
      
    int bufsize;   
    SensorRecord[] store;   
    int numberOfEntries = 0;   
    int front = 0;   
    int back = 0;   
      
    CircularBuffer(int n){   
        bufsize = n;   
        store =  new SensorRecord[bufsize];   
           
    }   
      
    /**  
     * 存放数据  
     * @param rec要存放的数据对象  
     * @throws InterruptedException  
     */  
    synchronized void put(SensorRecord rec) throws InterruptedException{   
        if(numberOfEntries == bufsize)   
            wait();   
        store[back] = new SensorRecord(rec.num, rec.degree);   
        System.out.println("put " + rec.toString());   
        back = back + 1;   
        if(back ==  bufsize)   
            back = 0;   
        numberOfEntries += 1;   
        notify();   
    }   
    /**  
     * 取出数据  
     * @return  
     * @throws InterruptedException  
     */  
    synchronized SensorRecord get() throws InterruptedException{   
        SensorRecord result = new SensorRecord(-1,-1);   
        if( 0 == numberOfEntries )   
            wait();   
        result = store[front];   
        System.out.println("get " + result.toString());   
        front += 1;   
        if(front == bufsize)   
            front = 0;   
        numberOfEntries -= 1;   
        notify();   
        return result;   
               
    }   
  
}  
package app;


public class CircularBuffer {

       
        int bufsize;
        SensorRecord[] store;
        int numberOfEntries = 0;
        int front = 0;
        int back = 0;
       
        CircularBuffer(int n){
                bufsize = n;
                store =  new SensorRecord[bufsize];
               
        }
       
        /**
         * 存放数据
         * @param rec要存放的数据对象
         * @throws InterruptedException
         */
        synchronized void put(SensorRecord rec) throws InterruptedException{
                if(numberOfEntries == bufsize)
                        wait();
                store[back] = new SensorRecord(rec.num, rec.degree);
                System.out.println("put " + rec.toString());
                back = back + 1;
                if(back ==  bufsize)
                        back = 0;
                numberOfEntries += 1;
                notify();
        }
        /**
         * 取出数据
         * @return
         * @throws InterruptedException
         */
        synchronized SensorRecord get() throws InterruptedException{
                SensorRecord result = new SensorRecord(-1,-1);
                if( 0 == numberOfEntries )
                        wait();
                result = store[front];
                System.out.println("get " + result.toString());
                front += 1;
                if(front == bufsize)
                        front = 0;
                numberOfEntries -= 1;
                notify();
                return result;
                       
        }

}
  

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

view plaincopy to clipboardprint?
package app;   
  
public class BufferPool {   
      
    public static CircularBuffer  buf = new CircularBuffer(100);   
  
}   
  
package app;   
  
public class Get implements Runnable {   
  
  
    public void run() {   
        while (true) {   
            try {   
                Thread.sleep(1000);   
                BufferPool.buf.get();   
            } catch (InterruptedException e) {   
                // TODO Auto-generated catch block   
                e.printStackTrace();   
            }   
        }   
    }   
  
  
}   
  
  
package app;   
  
public class Put implements Runnable {   
      
    public void run() {   
        while (true) {   
            int num = (int) (Math.random() * 1000);   
            int degree = (int) (Math.random() * 1000);   
            SensorRecord rec = new SensorRecord(num, degree);   
            try {   
                Thread.sleep(10);   
                BufferPool.buf.put(rec);   
            } catch (InterruptedException e) {   
                // TODO Auto-generated catch block   
                e.printStackTrace();   
            }   
        }   
    }   
  
}   
  
  
package app;   
  
public class SensorRecord {   
      
    public SensorRecord(int num2, int degree2) {   
        // TODO Auto-generated constructor stub   
        this.num = num2;   
        this.degree = degree2;   
    }   
      
    int num;   
    int degree;   
      
    public String  toString(){   
        return new String("num: " + num  + "; degree: " + degree);   
    }   
  
}   
  
  
package app;   
  
public class TestBuffer {   
  
    /**  
     * @param args  
     */  
      
      
    public static void main(String[] args) {   
        Get get = new Get();   
        Put put = new Put();   
        Thread thread = new Thread(get);   
        Thread thread2 = new Thread(put);   
        thread.start();   
        thread2.start();   
  
    }   
}  
package app;

public class BufferPool {
       
        public static CircularBuffer  buf = new CircularBuffer(100);

}

package app;

public class Get implements Runnable {


        public void run() {
                while (true) {
                        try {
                                Thread.sleep(1000);
                                BufferPool.buf.get();
                        } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                }
        }


}


package app;

public class Put implements Runnable {
       
        public void run() {
                while (true) {
                        int num = (int) (Math.random() * 1000);
                        int degree = (int) (Math.random() * 1000);
                        SensorRecord rec = new SensorRecord(num, degree);
                        try {
                                Thread.sleep(10);
                                BufferPool.buf.put(rec);
                        } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                }
        }

}


package app;

public class SensorRecord {
       
        public SensorRecord(int num2, int degree2) {
                // TODO Auto-generated constructor stub
                this.num = num2;
                this.degree = degree2;
        }
       
        int num;
        int degree;
       
        public String  toString(){
                return new String("num: " + num  + "; degree: " + degree);
        }

}


package app;

public class TestBuffer {

        /**
         * @param args
         */
       
       
        public static void main(String[] args) {
                Get get = new Get();
                Put put = new Put();
                Thread thread = new Thread(get);
                Thread thread2 = new Thread(put);
                thread.start();
                thread2.start();

        }
}








本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/jicheng687/archive/2009/12/11/4987357.aspx
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP