diff --git a/ErzeugerVerbraucher/src/Buffer.java b/ErzeugerVerbraucher/src/Buffer.java index a1101dcfc5302ca275ec6272afb2635cb6babcf3..3ab26b2585ab3ee652e7febdead68a37dc93ec1b 100644 --- a/ErzeugerVerbraucher/src/Buffer.java +++ b/ErzeugerVerbraucher/src/Buffer.java @@ -26,10 +26,13 @@ public class Buffer<T> { * @throws IllegalStateException Thrown if pushing would exceed the maximum allowed size */ public void push(T item) throws IllegalStateException { + if (this.itemCount.get() >= this.size) { throw new IllegalStateException(); } + + this.items.set(this.itemCount.get(), item); this.itemCount.incrementAndGet(); } @@ -37,10 +40,11 @@ public class Buffer<T> { /** * Pop an item from the Buffer * @return The item to be popped + * * @throws IllegalStateException Thrown if popping would underflow the minimum allowed size */ public T pop() throws IllegalStateException { - if (this.itemCount.get() <= 0) { /// ORIGINAL <1 + if (this.itemCount.get() <= 0) { throw new IllegalStateException(); } diff --git a/ErzeugerVerbraucher/src/Consumer.java b/ErzeugerVerbraucher/src/Consumer.java index e576eb84c11d90af2ea54dcf216111583a37cfc4..4cf398c7e2f98b3ba6bebf8d4bcf1eb1af75c013 100644 --- a/ErzeugerVerbraucher/src/Consumer.java +++ b/ErzeugerVerbraucher/src/Consumer.java @@ -7,6 +7,13 @@ public class Consumer implements Runnable { private final AtomicBoolean asleep; private final AtomicBoolean wakeCall; + /** + * Constructor for Consumer data structure + * @param buffer Buffer instance for Consumer + * @param interval interval of Consumer + * @param asleep Boolean for Status + * @param wakeCall Boolean for Status + */ public Consumer(Buffer<Car> buffer, int interval, AtomicBoolean asleep, AtomicBoolean wakeCall) { this.buffer = buffer; this.interval = interval; @@ -14,6 +21,12 @@ public class Consumer implements Runnable { this.wakeCall = wakeCall; } + /** + * Run function for the Consumer + * This Function checks randomly if the buffer has Cars and Pops them if that is the case. + * If the buffer is full it wakens the sleeping consumers. + * + */ @Override public void run() { while(true) { diff --git a/ErzeugerVerbraucher/src/Main.java b/ErzeugerVerbraucher/src/Main.java index c84216147c5d931cb9183741807851e646f8c6fc..755a7643f43e7318956d4d90c8e526f0e0b2cd29 100644 --- a/ErzeugerVerbraucher/src/Main.java +++ b/ErzeugerVerbraucher/src/Main.java @@ -1,5 +1,8 @@ import java.util.concurrent.atomic.AtomicBoolean; +/** + * Main to show the functionalities of this Implementation + */ public class Main { public static void main(String[] args) { Buffer<Car> buffer = new Buffer<>(5); diff --git a/ErzeugerVerbraucher/src/Producer.java b/ErzeugerVerbraucher/src/Producer.java index 2f89cf44cf75b28ab6285d544bc0160eb161b0a9..716b1907bda481ce8fa340fc2c4c33d8fa01b5c7 100644 --- a/ErzeugerVerbraucher/src/Producer.java +++ b/ErzeugerVerbraucher/src/Producer.java @@ -7,6 +7,13 @@ public class Producer implements Runnable { private final AtomicBoolean asleep; private final AtomicBoolean wakeCall; + /** + * Constructor for Consumer data structure + * @param buffer Buffer instance for Producer + * @param interval interval of Consumer + * @param asleep Boolean for Status + * @param wakeCall Boolean for Status + */ public Producer(Buffer<Car> buffer, int interval, AtomicBoolean asleep, AtomicBoolean wakeCall) { this.buffer = buffer; this.interval = interval; @@ -14,6 +21,11 @@ public class Producer implements Runnable { this.wakeCall = wakeCall; } + /** + * Run Function for the Producer + * This function Produces Cars randomly when the Buffer is not full. + * If the Buffer is empty it wakens the Sleeping Producers + */ @Override public void run() { while(true) {