Skip to content
Snippets Groups Projects
Commit 855dee64 authored by Juan Lauer Garrido's avatar Juan Lauer Garrido
Browse files

Comments Added

parent fcbf8e31
No related branches found
No related tags found
No related merge requests found
......@@ -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();
}
......
......@@ -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) {
......
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);
......
......@@ -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) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment