Skip to content
Snippets Groups Projects
Commit 7b0dc8e7 authored by rasmusse's avatar rasmusse
Browse files

Push new Stuff

parent 03df4412
No related branches found
No related tags found
No related merge requests found
......@@ -3,4 +3,9 @@
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" project-jdk-name="openjdk-18" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
<component name="SwUserDefinedSpecifications">
<option name="specTypeByUrl">
<map />
</option>
</component>
</project>
\ No newline at end of file
import java.nio.BufferOverflowException;
import java.nio.BufferUnderflowException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Semaphore;
import java.lang.Thread;
public class Buffer<T>{
public int size;
public Semaphore mutex;
List<T> items = new ArrayList<T>();
import java.lang.*;
public Buffer(int size) {
this.size= size;
//BufferOVerflowException, wenn Buffer voll ist und dennoch Element geschrieben werden will
public void push(Buffer<T> e){
if()
mutex.acquire();
items.add(T.e);
mutex.release();
public class Buffer<T> {
int size;
public Semaphore sem;
public ArrayList<T> items = new ArrayList<T>(size);
public Buffer(int size){
assert size >= 0;
this.size = size;
}
//BufferUnderflowException wenn Buffer leer ist und dennoch Element gelesen werden will
public void pop(Buffer<T> e){
public void push(T e) throws InterruptedException {
assert(e!=null);
assert(!full());
if(this.full()) {
System.out.println("Buffer is full");
}
this.sem.acquire();
items.add(e);
this.sem.release();
}
public boolean full(){
assert items.size()>=0 : "Buffer cannot be less than 0 in size";
// 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();
public boolean empty(){
return is_full;
}
public boolean empty() throws InterruptedException {
sem.acquire();
boolean is_empty = items.size()==0;
sem.release();
return is_empty;
}
}
}
public class Consumer {
import com.sun.jdi.event.ThreadDeathEvent;
import com.sun.jdi.event.ThreadStartEvent;
import java.util.Random;
import java.lang.*;
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;
this.buffer = buffer;
this.interval = interval;
this.consumerThread = consumerThread;
this.producerThread = producerThread;
this.run();
assert this.interval == interval;
}
public void run() throws InterruptedException{
assert this.interval > 0;
while(this.interval > 0) {
Thread.sleep(new Random().nextInt(interval));
boolean was_full = this.buffer.full();
assert was_full instanceof
if(!this.consumerThread.)
}
}
}
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 b1 = new Buffer(5); //Capacity of the Buffer
}
}
\ No newline at end of file
}
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