commit 0233f9f629d17fec510f8f2e652cde33cf0a5fff Author: 2wenty1ne Date: Tue Oct 22 16:42:14 2024 +0200 c diff --git a/src/src/BoundedBuffer.java b/src/src/BoundedBuffer.java new file mode 100644 index 0000000..58d0d8b --- /dev/null +++ b/src/src/BoundedBuffer.java @@ -0,0 +1,80 @@ +public class BoundedBuffer { + + final Object[] items = new Object[8]; + int putptr, takeptr, count; + + public void put(T x) throws InterruptedException { + // solange der Speicher voll ist, warten + this.items[this.putptr] = x; + if (++this.putptr == this.items.length) { + this.putptr = 0; + } + ++this.count; + // nun ist etwas neues im Speicher: Alle wartenden Threads + // benachrichtigen + } + + public T take() throws InterruptedException { + // solange der Speicher leer ist, warten + @SuppressWarnings("unchecked") + var x = (T) this.items[this.takeptr]; + if (++this.takeptr == this.items.length) { + this.takeptr = 0; + } + --this.count; + // nun ist etwas Platz im Speicher: Alle wartenden Threads + // benachrichtigen + return x; + } + + public static void main(String... args) throws InterruptedException { + var mem = new BoundedBuffer(); + var p1 = new Thread( + () -> { + try { + for (var j = 1; j <= 10; j++) { + mem.put(j); + System.out.println(Thread.currentThread().getName() + ": put=" + j); + Thread.sleep(1000); + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + }, + "Producer-1" + ); + var p2 = new Thread( + () -> { + try { + for (var j = 1; j <= 10; j++) { + mem.put(j); + System.out.println(Thread.currentThread().getName() + ": put=" + j); + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + }, + "Producer-2" + ); + var c1 = new Thread( + () -> { + try { + for (var j = 1; j <= 20; j++) { + System.out.println(Thread.currentThread().getName() + ": taken=" + mem.take()); + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + }, + "Consumer-1" + ); + var now = System.currentTimeMillis(); + p1.start(); + p2.start(); + c1.start(); + p1.join(); + p2.join(); + c1.join(); + System.out.println("Runtime: " + (System.currentTimeMillis() - now) + "ms"); + } +} \ No newline at end of file