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 0000000000000000000000000000000000000000..ddf41a1f16998f6bdc632d5a25139b9e2705ac53 --- /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 5c3d45d3a5129e1c275d7c5d3f60d1bb4b8ff1c6..8aa155c6eaf3923d2992645318b35c25ed5947a1 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 d2929ca45be9a793f86f6f7ff5a0d3f469216be2..05bc07186cbe3a39c90093b6c8bf7da329c7709c 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 0af7d67aaaf4bde3b987737352ac4c314afc8049..34c9fd28033ba9c227cd01f7824b0723eef0478f 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>