GenericMonitor: Replaced by CircularBuffer
This commit is contained in:
@@ -1,6 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<classpath>
|
|
||||||
<classpathentry kind="src" path="src"/>
|
|
||||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
|
||||||
<classpathentry kind="output" path="bin"/>
|
|
||||||
</classpath>
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<projectDescription>
|
|
||||||
<name>GenericMonitor</name>
|
|
||||||
<comment></comment>
|
|
||||||
<projects>
|
|
||||||
</projects>
|
|
||||||
<buildSpec>
|
|
||||||
<buildCommand>
|
|
||||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
|
||||||
<arguments>
|
|
||||||
</arguments>
|
|
||||||
</buildCommand>
|
|
||||||
</buildSpec>
|
|
||||||
<natures>
|
|
||||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
|
||||||
</natures>
|
|
||||||
</projectDescription>
|
|
||||||
@@ -1,48 +0,0 @@
|
|||||||
|
|
||||||
// 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 = {-1, -2, -3, -4};
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
|
|
||||||
public class GenericMonitor {
|
|
||||||
|
|
||||||
static Buffer MyBuffer = new Buffer();
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
// TODO Auto-generated method stub
|
|
||||||
|
|
||||||
new Thread(() -> {
|
|
||||||
System.out.println("Producer running");
|
|
||||||
for ( int nn=0; nn < 20; nn++) {
|
|
||||||
MyBuffer.Write( nn );
|
|
||||||
System.out.println("Producer Wrote " + nn);
|
|
||||||
}
|
|
||||||
}).start();
|
|
||||||
new Thread(() -> {
|
|
||||||
System.out.println("Consumer running");
|
|
||||||
for ( int nn=0; nn < 20; nn++ ) {
|
|
||||||
System.out.println("Consumer Read " + MyBuffer.Read());
|
|
||||||
}
|
|
||||||
}).start();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user