From 35045dd0fadca87152d346d81ba7d0523351e48f Mon Sep 17 00:00:00 2001
From: Tobias Jordine <jordine@hdm-stuttgart.de>
Date: Tue, 14 Jun 2022 21:30:38 +0200
Subject: [PATCH] Vorlesung 13.06.2022

---
 .../concurrency/issues/BlockingQueue.java     | 80 +++++++++++++++++++
 .../concurrency/issues/LostUpdate.java        | 13 ++-
 .../solutions/LostUpdateSynchronized.java     | 15 ++--
 website/index.html                            |  1 +
 4 files changed, 100 insertions(+), 9 deletions(-)
 create mode 100644 src/main/java/de/hdm/jordine/vorlesung/concurrency/issues/BlockingQueue.java

diff --git a/src/main/java/de/hdm/jordine/vorlesung/concurrency/issues/BlockingQueue.java b/src/main/java/de/hdm/jordine/vorlesung/concurrency/issues/BlockingQueue.java
new file mode 100644
index 0000000..ddf41a1
--- /dev/null
+++ b/src/main/java/de/hdm/jordine/vorlesung/concurrency/issues/BlockingQueue.java
@@ -0,0 +1,80 @@
+package de.hdm.jordine.vorlesung.concurrency.issues;
+
+import java.util.LinkedList;
+import java.util.Queue;
+
+    public class BlockingQueue<T> {
+        private Queue<T> queue = new LinkedList<T>();
+        private int capacity;
+        public BlockingQueue(int capacity) {
+            this.capacity = capacity;
+        }
+        public synchronized void put(T element) throws InterruptedException {
+            while(queue.size() == capacity) {
+                System.out.println("waitng to add");
+                wait();
+
+            }
+            queue.add(element);
+            notify(); // notifyAll() for multiple producer/consumer threads
+        }
+        public synchronized T take() throws InterruptedException {
+            while(queue.isEmpty()) {
+                System.out.println("waitng to remove");
+                wait();
+            }
+            T item = queue.remove();
+            notify(); // notifyAll() for multiple producer/consumer threads
+            return item;
+        }
+
+        public static void main(String[] args) {
+
+
+            BlockingQueue<String> b = new BlockingQueue<>(1);
+
+            Thread t0 = new Thread(new Runnable() {
+                @Override
+                public void run() {
+                    try {
+                        b.put("Hello");
+                        System.out.println("adding hello");
+                    } catch (InterruptedException e) {
+                        e.printStackTrace();
+                    }
+                }
+            });
+
+            t0.start();
+
+            Thread t1 = new Thread(new Runnable() {
+                @Override
+                public void run() {
+                    try {
+                        b.put("World");
+                        System.out.println("adding World");
+                    } catch (InterruptedException e) {
+                        e.printStackTrace();
+                    }
+                }
+            });
+
+            t1.start();
+
+            Thread t2 = new Thread(new Runnable() {
+                @Override
+                public void run() {
+                    try {
+                        String text = b.take();
+                        System.out.println("removing " + text);
+                    } catch (InterruptedException e) {
+                        e.printStackTrace();
+                    }
+                }
+            });
+
+            t2.start();
+
+        }
+    }
+
diff --git a/src/main/java/de/hdm/jordine/vorlesung/concurrency/issues/LostUpdate.java b/src/main/java/de/hdm/jordine/vorlesung/concurrency/issues/LostUpdate.java
index 5c3d45d..8aa155c 100644
--- a/src/main/java/de/hdm/jordine/vorlesung/concurrency/issues/LostUpdate.java
+++ b/src/main/java/de/hdm/jordine/vorlesung/concurrency/issues/LostUpdate.java
@@ -2,10 +2,11 @@ package de.hdm.jordine.vorlesung.concurrency.issues;
 
 public class LostUpdate {
 
+    //private volatile int counter = 0;
     private int counter = 0;
 
     public void increment(){
-        counter++;
+        counter = counter + 1;
     }
 
     public static void main(String[] args) throws InterruptedException {
@@ -30,13 +31,19 @@ public class LostUpdate {
             }
         });
 
+        long start = System.currentTimeMillis();
+
         t0.start();
         t1.start();
 
-        //t0.join();
-        //t1.join();
+        // t0.join(); // required for synchronisation with main thread
+        // t1.join(); // required for synchronisation with main thread
+
+        long end = System.currentTimeMillis();
 
         System.out.println("Counter value: " + lu.counter);
+
+        System.out.println(end - start);
     }
 
 
diff --git a/src/main/java/de/hdm/jordine/vorlesung/concurrency/solutions/LostUpdateSynchronized.java b/src/main/java/de/hdm/jordine/vorlesung/concurrency/solutions/LostUpdateSynchronized.java
index d2929ca..05bc071 100644
--- a/src/main/java/de/hdm/jordine/vorlesung/concurrency/solutions/LostUpdateSynchronized.java
+++ b/src/main/java/de/hdm/jordine/vorlesung/concurrency/solutions/LostUpdateSynchronized.java
@@ -4,15 +4,12 @@ public class LostUpdateSynchronized {
 
     private int counter = 0;
 
-    public void increment(){
+    public synchronized void increment(){
         counter++;
     }
 
     public static void main(String[] args) throws InterruptedException {
 
-        Object monitor0 = new Object();
-        Object monitor1 = new Object();
-
         LostUpdateSynchronized lu = new LostUpdateSynchronized();
 
         Thread t0 = new Thread(new Runnable() {
@@ -33,13 +30,19 @@ public class LostUpdateSynchronized {
             }
         });
 
+        long start = System.currentTimeMillis();
+
         t0.start();
         t1.start();
 
-        //t0.join();
-        //t1.join();
+        t0.join(); // required for synchronisation with main thread
+        t1.join(); // required for synchronisation with main thread
+
+        long end = System.currentTimeMillis();
 
         System.out.println("Counter value: " + lu.counter);
+
+        System.out.println(end - start);
     }
 
 
diff --git a/website/index.html b/website/index.html
index 0af7d67..34c9fd2 100644
--- a/website/index.html
+++ b/website/index.html
@@ -71,6 +71,7 @@
         <li><a href="https://bigbluebutton.hdm-stuttgart.de/playback/presentation/2.3/cf93de2bb79bc2a9a9374f2f59a28fe89d96716b-1652709634577" target="_blank">16.05.: CI/CD, SQL</a></li>
         <li><a href="https://bigbluebutton.hdm-stuttgart.de/playback/presentation/2.3/cf93de2bb79bc2a9a9374f2f59a28fe89d96716b-1653313987460" target="_blank">23.05.: SQL, NoSQL, Schnittstellen</a></li>
         <li><a href="https://bigbluebutton.hdm-stuttgart.de/playback/presentation/2.3/cf93de2bb79bc2a9a9374f2f59a28fe89d96716b-1653940765288" target="_blank">30.05.: Schnittstellen (Nachaufzeichnung)</a></li>
+        <li><a href="https://bigbluebutton.hdm-stuttgart.de/playback/presentation/2.3/cf93de2bb79bc2a9a9374f2f59a28fe89d96716b-1655128617403" target="_blank">13.06.: Parallele Programmierung</a></li>
     </ul>
 
     <h4>Tafelbilder</h4>
-- 
GitLab