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.Semaphore;
import java.lang.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReferenceArray;
/**
* 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> {
AtomicReferenceArray<T> items;
AtomicInteger itemCount;
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;
}
public void push(T e) throws InterruptedException {
assert(e!=null);
assert(!full());
if(this.full()) {
System.out.println("Buffer is full");
/**
* Push an item to the Buffer
* @param item The item to be pushed
* @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.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();
// item = items.remove(0);
// sem.release();
// return item;
//
// }
public boolean full() throws InterruptedException {
sem.acquire();
boolean is_full = items.size()==size;
sem.release();
return is_full;
/**
* 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
throw new IllegalStateException();
}
return this.items.getAndSet(this.itemCount.getAndDecrement(), null);
}
public boolean empty() throws InterruptedException {
sem.acquire();
boolean is_empty = items.size()==0;
sem.release();
return is_empty;
/**
* Check whether the Buffer is full
* @return True if the Buffer is full, False otherwise
*/
public boolean full() {
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 {
}
import com.sun.jdi.event.ThreadDeathEvent;
import com.sun.jdi.event.ThreadStartEvent;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.Random;
import java.lang.*;
public class Consumer implements Runnable {
private final Buffer<Car> buffer;
private final int interval;
private final AtomicBoolean asleep;
private final AtomicBoolean wakeCall;
public class Consumer{
int interval;
ThreadDeathEvent consumerThread;
ThreadStartEvent producerThread;
Buffer buffer;
public Consumer(Buffer buffer, int interval, ThreadDeathEvent consumerThread, ThreadStartEvent producerThread) {
assert buffer != null;
assert interval > 0;
public Consumer(Buffer<Car> buffer, int interval, AtomicBoolean asleep, AtomicBoolean wakeCall) {
this.buffer = buffer;
this.interval = interval;
this.consumerThread = consumerThread;
this.producerThread = producerThread;
this.run();
assert this.interval == interval;
this.asleep = asleep;
this.wakeCall = wakeCall;
}
public void run() throws InterruptedException{
assert this.interval > 0;
while(this.interval > 0) {
Thread.sleep(new Random().nextInt(interval));
@Override
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();
assert was_full instanceof
if(!this.consumerThread.)
if (wasFull) {
this.wakeCall.set(false);
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
import java.util.concurrent.atomic.AtomicBoolean;
public class Main {
public static void main(String[] args) {
Buffer b1 = new Buffer(5);
try {
b1.push(5);
} catch (InterruptedException e) {
System.out.println(e);
Buffer<Car> buffer = new Buffer<>(5);
AtomicBoolean consumerStatus = new AtomicBoolean(false);
AtomicBoolean producerStatus = new AtomicBoolean(false);
int consumerCount = Integer.parseInt(args[0]);
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