在數據采取時經常用戶緩沖器來暫時存放數據顯然此時一定要有一個相互排斥機制以防止生產者和消費者進程同時對這個緩沖器中的同一個元素進行存取同時系統還要確保緩沖器已滿時生產者進程不再試著往裡添加信息消費者進程在緩沖器為空時也不去取信息
具體實現如下
view plaincopy to clipboardprint?
package app
public class CircularBuffer {
int bufsizeSensorRecord[] storeint numberOfEntries = int front = int back =
CircularBuffer(int n){ bufsize = nstore = new SensorRecord[bufsize]
}
/** * 存放數據* @param rec要存放的數據對象* @throws InterruptedException */ synchronized void put(SensorRecord rec) throws InterruptedException{ if(numberOfEntries == bufsize)
wait()store[back] = new SensorRecord(recnum recdegree)Systemoutprintln(put + rectoString())back = back + if(back == bufsize)
back = numberOfEntries += notify()} /** * 取出數據* @return * @throws InterruptedException */ synchronized SensorRecord get() throws InterruptedException{ SensorRecord result = new SensorRecord()if( == numberOfEntries )
wait()result = store[front]Systemoutprintln(get + resulttoString())front += if(front == bufsize)
front = numberOfEntries = notify()return result
}
} package app
public class CircularBuffer {
int bufsizeSensorRecord[] storeint numberOfEntries = int front = int back =
CircularBuffer(int n){ bufsize = nstore = new SensorRecord[bufsize]
}
/** * 存放數據* @param rec要存放的數據對象* @throws InterruptedException */ synchronized void put(SensorRecord rec) throws InterruptedException{ if(numberOfEntries == bufsize)
wait()store[back] = new SensorRecord(recnum recdegree)Systemoutprintln(put + rectoString())back = back + if(back == bufsize)
back = numberOfEntries += notify()} /** * 取出數據* @return * @throws InterruptedException */ synchronized SensorRecord get() throws InterruptedException{ SensorRecord result = new SensorRecord()if( == numberOfEntries )
wait()result = store[front]Systemoutprintln(get + resulttoString())front += if(front == bufsize)
front = numberOfEntries = notify()return result
}
}
完整代碼如下(僅供學習參考)
view plaincopy to clipboardprint?
package app
public class BufferPool {
public static CircularBuffer buf = new CircularBuffer()
}
package app
public class Get implements Runnable {
public void run() { while (true) { try { Threadsleep()BufferPoolbufget()} catch (InterruptedException e) { // TODO Autogenerated catch block eprintStackTrace()}
}
package app
public class Put implements Runnable {
public void run() { while (true) { int num = (int) (Mathrandom() * )int degree = (int) (Mathrandom() * )SensorRecord rec = new SensorRecord(num degree)try { Threadsleep()BufferPoolbufput(rec)} catch (InterruptedException e) { // TODO Autogenerated catch block eprintStackTrace()}
}
package app
public class SensorRecord {
public SensorRecord(int num int degree) { // TODO Autogenerated constructor stub thisnum = numthisdegree = degree}
int numint 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 thread = new Thread(put)threadstart()threadstart()
} package app
public class BufferPool {
public static CircularBuffer buf = new CircularBuffer()
}
package app
public class Get implements Runnable {
public void run() { while (true) { try { Threadsleep()BufferPoolbufget()} catch (InterruptedException e) { // TODO Autogenerated catch block eprintStackTrace()}
}
package app
public class Put implements Runnable {
public void run() { while (true) { int num = (int) (Mathrandom() * )int degree = (int) (Mathrandom() * )SensorRecord rec = new SensorRecord(num degree)try { Threadsleep()BufferPoolbufput(rec)} catch (InterruptedException e) { // TODO Autogenerated catch block eprintStackTrace()}
}
package app
public class SensorRecord {
public SensorRecord(int num int degree) { // TODO Autogenerated constructor stub thisnum = numthisdegree = degree}
int numint 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 thread = new Thread(put)threadstart()threadstart()
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25728.html