2025-02-15

在數據采取時,經常用戶緩沖器來暫時存放數據,顯然,此時一定要有一個相互排斥機制以防止生產者和消費者進程同時對這個緩沖器中的同一個元素進行存取。同時,系統還要確保緩沖器已滿時生產者進程不再試著往裡添加信息,消費者進程在緩沖器為空時也不去取信息。
  具體實現如下:
  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();
  }

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *