From d363a3e30a58709b147988bacef7a5ece8cc6de1 Mon Sep 17 00:00:00 2001 From: Karsten Jeppesen Date: Tue, 2 Mar 2021 17:19:45 +0100 Subject: [PATCH] GenericMonitor: Replaced by CircularBuffer --- GenericMonitor/.classpath | 6 ---- GenericMonitor/.project | 17 --------- GenericMonitor/src/Buffer.java | 48 -------------------------- GenericMonitor/src/GenericMonitor.java | 25 -------------- 4 files changed, 96 deletions(-) delete mode 100644 GenericMonitor/.classpath delete mode 100644 GenericMonitor/.project delete mode 100644 GenericMonitor/src/Buffer.java delete mode 100644 GenericMonitor/src/GenericMonitor.java diff --git a/GenericMonitor/.classpath b/GenericMonitor/.classpath deleted file mode 100644 index fb50116..0000000 --- a/GenericMonitor/.classpath +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - diff --git a/GenericMonitor/.project b/GenericMonitor/.project deleted file mode 100644 index 17c9a86..0000000 --- a/GenericMonitor/.project +++ /dev/null @@ -1,17 +0,0 @@ - - - GenericMonitor - - - - - - org.eclipse.jdt.core.javabuilder - - - - - - org.eclipse.jdt.core.javanature - - diff --git a/GenericMonitor/src/Buffer.java b/GenericMonitor/src/Buffer.java deleted file mode 100644 index 29d92fe..0000000 --- a/GenericMonitor/src/Buffer.java +++ /dev/null @@ -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; - } - } - } -} diff --git a/GenericMonitor/src/GenericMonitor.java b/GenericMonitor/src/GenericMonitor.java deleted file mode 100644 index a2a8b68..0000000 --- a/GenericMonitor/src/GenericMonitor.java +++ /dev/null @@ -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(); - - } - -}