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

Push new status

parent 7b0dc8e7
No related branches found
No related tags found
No related merge requests found
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>
\ No newline at end of file
File deleted
import java.util.ArrayList; import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.Semaphore; import java.util.concurrent.atomic.AtomicReferenceArray;
import java.lang.*;
/**
* A generic and atomically safe Buffer data structure for holding values important in multithreaded use cases.
* @param <T> The type of item to hold within the Buffer
*/
public class Buffer<T> { public class Buffer<T> {
AtomicReferenceArray<T> items;
AtomicInteger itemCount;
int size; int size;
public Semaphore sem;
public ArrayList<T> items = new ArrayList<T>(size);
public Buffer(int size){ /**
assert size >= 0; * Constructor for Buffer data structure
* @param size The size of the Buffer
*/
public Buffer(int size) {
this.items = new AtomicReferenceArray<>(size);
this.itemCount = new AtomicInteger(0);
this.size = size; this.size = size;
} }
public void push(T e) throws InterruptedException { /**
assert(e!=null); * Push an item to the Buffer
assert(!full()); * @param item The item to be pushed
if(this.full()) { * @throws IllegalStateException Thrown if pushing would exceed the maximum allowed size
System.out.println("Buffer is full"); */
public void push(T item) throws IllegalStateException {
if (this.itemCount.get() >= this.size) {
throw new IllegalStateException();
} }
this.sem.acquire();
items.add(e);
this.sem.release();
this.items.set(this.itemCount.get(), item);
this.itemCount.incrementAndGet();
} }
// public ArrayList<T> pop() throws InterruptedException{ /**
// sem.acquire(); * Pop an item from the Buffer
// item = items.remove(0); * @return The item to be popped
// sem.release(); * @throws IllegalStateException Thrown if popping would underflow the minimum allowed size
// return item; */
// public T pop() throws IllegalStateException {
// } if (this.itemCount.get() <= 0) { /// ORIGINAL <1
throw new IllegalStateException();
public boolean full() throws InterruptedException { }
sem.acquire();
boolean is_full = items.size()==size;
sem.release();
return is_full;
return this.items.getAndSet(this.itemCount.getAndDecrement(), null);
} }
public boolean empty() throws InterruptedException { /**
sem.acquire(); * Check whether the Buffer is full
boolean is_empty = items.size()==0; * @return True if the Buffer is full, False otherwise
sem.release(); */
public boolean full() {
return is_empty; return this.itemCount.get() >= this.size;
}
/**
* Check whether the Buffer is empty
* @return True if the Buffer is empty, False otherwise
*/
public boolean empty() {
return Integer.valueOf(this.itemCount.get()).equals(0);
} }
} }
public class Car { public class Car {
} }
import com.sun.jdi.event.ThreadDeathEvent; import java.util.concurrent.TimeUnit;
import com.sun.jdi.event.ThreadStartEvent; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.Random; public class Consumer implements Runnable {
import java.lang.*; private final Buffer<Car> buffer;
private final int interval;
private final AtomicBoolean asleep;
private final AtomicBoolean wakeCall;
public class Consumer{ public Consumer(Buffer<Car> buffer, int interval, AtomicBoolean asleep, AtomicBoolean wakeCall) {
int interval;
ThreadDeathEvent consumerThread;
ThreadStartEvent producerThread;
Buffer buffer;
public Consumer(Buffer buffer, int interval, ThreadDeathEvent consumerThread, ThreadStartEvent producerThread) {
assert buffer != null;
assert interval > 0;
this.buffer = buffer; this.buffer = buffer;
this.interval = interval; this.interval = interval;
this.consumerThread = consumerThread; this.asleep = asleep;
this.producerThread = producerThread; this.wakeCall = wakeCall;
this.run();
assert this.interval == interval;
} }
public void run() throws InterruptedException{
assert this.interval > 0;
while(this.interval > 0) { @Override
Thread.sleep(new Random().nextInt(interval)); public void run() {
while(true) {
try {
TimeUnit.SECONDS.sleep((int)(Math.random() * this.interval + 1));
boolean wasFull = this.buffer.full();
if (!this.asleep.get()) {
System.out.printf("%s consumer ...%n", Thread.currentThread().getId());
if (!this.buffer.empty()) {
this.buffer.pop();
System.out.printf(
"%s consumer TAKING OUT %s/%s %n",
Thread.currentThread().getId(),
this.buffer.itemCount,
this.buffer.size
);
} else {
this.asleep.set(true);
}
}
boolean was_full = this.buffer.full(); if (wasFull) {
assert was_full instanceof this.wakeCall.set(false);
if(!this.consumerThread.) }
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
} }
} }
} }
import java.util.concurrent.atomic.AtomicBoolean;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
Buffer b1 = new Buffer(5); Buffer<Car> buffer = new Buffer<>(5);
try { AtomicBoolean consumerStatus = new AtomicBoolean(false);
b1.push(5); AtomicBoolean producerStatus = new AtomicBoolean(false);
} catch (InterruptedException e) { int consumerCount = Integer.parseInt(args[0]);
System.out.println(e); int producerCount = Integer.parseInt(args[1]);
for (int i = 0; i < consumerCount; i++) {
Thread consumerThread = new Thread(new Runnable() {
@Override
public void run() {
Consumer consumer = new Consumer(buffer, 15, consumerStatus, producerStatus);
consumer.run();
}
});
consumerThread.start();
} }
for (int i = 0; i < producerCount; i++) {
Thread producerThread = new Thread(new Runnable() {
@Override
public void run() {
Producer producer = new Producer(buffer, 15, producerStatus, consumerStatus);
producer.run();
}
});
producerThread.start();
}
} }
} }
\ No newline at end of file
public class Producer { import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
public class Producer implements Runnable {
private final Buffer<Car> buffer;
private final int interval;
private final AtomicBoolean asleep;
private final AtomicBoolean wakeCall;
public Producer(Buffer<Car> buffer, int interval, AtomicBoolean asleep, AtomicBoolean wakeCall) {
this.buffer = buffer;
this.interval = interval;
this.asleep = asleep;
this.wakeCall = wakeCall;
}
@Override
public void run() {
while(true) {
try {
TimeUnit.SECONDS.sleep((int)(Math.random() * this.interval + 1));
boolean wasEmpty = this.buffer.empty();
if (!this.asleep.get()) {
System.out.printf("%s producer ...%n", Thread.currentThread().getId());
if (!this.buffer.full()) {
this.buffer.push(new Car());
System.out.printf(
"%s producer PUSHING IN %s/%s %n",
Thread.currentThread().getId(),
this.buffer.itemCount,
this.buffer.size
);
} else {
this.asleep.set(true);
}
}
if (wasEmpty) {
this.wakeCall.set(false);
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
} }
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