在数据采取时经常用户缓沖器来暂时存放数据显然此时一定要有一个相互排斥机制以防止生产者和消费者进程同时对这个缓沖器中的同一个元素进行存取同时系统还要确保缓沖器已满时生产者进程不再试着往里添加信息消费者进程在缓沖器为空时也不去取信息
具体实现如下
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()
}