From 3f7dc9712cdae3acac56c6fa2c65d14ddf8580dc Mon Sep 17 00:00:00 2001 From: Karsten Jeppesen Date: Tue, 2 Mar 2021 16:56:04 +0100 Subject: [PATCH] GeneralMonitor - using synchronized methods and lambda threads --- GenericMonitor/.classpath | 6 ++++ GenericMonitor/.project | 17 +++++++++ GenericMonitor/src/Buffer.java | 48 ++++++++++++++++++++++++++ GenericMonitor/src/GenericMonitor.java | 25 ++++++++++++++ 4 files changed, 96 insertions(+) create mode 100644 GenericMonitor/.classpath create mode 100644 GenericMonitor/.project create mode 100644 GenericMonitor/src/Buffer.java create mode 100644 GenericMonitor/src/GenericMonitor.java diff --git a/GenericMonitor/.classpath b/GenericMonitor/.classpath new file mode 100644 index 0000000..fb50116 --- /dev/null +++ b/GenericMonitor/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/GenericMonitor/.project b/GenericMonitor/.project new file mode 100644 index 0000000..17c9a86 --- /dev/null +++ b/GenericMonitor/.project @@ -0,0 +1,17 @@ + + + GenericMonitor + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/GenericMonitor/src/Buffer.java b/GenericMonitor/src/Buffer.java new file mode 100644 index 0000000..29d92fe --- /dev/null +++ b/GenericMonitor/src/Buffer.java @@ -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; + } + } + } +} diff --git a/GenericMonitor/src/GenericMonitor.java b/GenericMonitor/src/GenericMonitor.java new file mode 100644 index 0000000..a2a8b68 --- /dev/null +++ b/GenericMonitor/src/GenericMonitor.java @@ -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(); + + } + +}