CircularBuffer: Nice buffer with lambda producer and consumer
This commit is contained in:
48
CircularBuffer/src/Buffer.java
Normal file
48
CircularBuffer/src/Buffer.java
Normal file
@@ -0,0 +1,48 @@
|
||||
|
||||
// All non primary objects (int, char etc) have wait, notify, notifyAll
|
||||
// Methods "synchronized" to provide intrinsic locks
|
||||
// If a thread calling wait() method does not own the inherent lock,
|
||||
// an error will be thrown.
|
||||
public class Buffer {
|
||||
private int BufferSize = 4;
|
||||
private int[] Container = new int[BufferSize];
|
||||
private int PosR=0, PosW=0;
|
||||
|
||||
public synchronized int Read() {
|
||||
while (true) {
|
||||
if ( PosR == PosW ) {
|
||||
try {
|
||||
wait();
|
||||
} catch (InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
int v=Container[ PosR ];
|
||||
PosR = (PosR+1) % BufferSize;
|
||||
System.out.println("Read pos " + PosR);
|
||||
notifyAll();
|
||||
return v;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void Write( int Val ) {
|
||||
while (true) {
|
||||
if (((PosW + 1) % BufferSize) == PosR) {
|
||||
try {
|
||||
wait();
|
||||
} catch (InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
Container[PosW] = Val;
|
||||
PosW = (PosW+1) % BufferSize;
|
||||
System.out.println("Write pos " + PosW);
|
||||
notifyAll();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user