GeneralMonitor - using synchronized methods and lambda threads
This commit is contained in:
6
GenericMonitor/.classpath
Normal file
6
GenericMonitor/.classpath
Normal file
@@ -0,0 +1,6 @@
|
||||
<?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>
|
||||
17
GenericMonitor/.project
Normal file
17
GenericMonitor/.project
Normal file
@@ -0,0 +1,17 @@
|
||||
<?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>
|
||||
48
GenericMonitor/src/Buffer.java
Normal file
48
GenericMonitor/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 = {-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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
25
GenericMonitor/src/GenericMonitor.java
Normal file
25
GenericMonitor/src/GenericMonitor.java
Normal file
@@ -0,0 +1,25 @@
|
||||
|
||||
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