Compare commits

...

3 Commits

Author SHA1 Message Date
Matti
b5d79e2b57 Parallel works as long as numUsers >= numPrinters 2024-06-03 15:16:33 +02:00
Matti
eadcc6fe1c Parallel adding kinda works 2024-05-31 14:29:43 +02:00
Matti
b67396d350 Add 4 1 2024-05-30 19:38:33 +02:00
7 changed files with 157 additions and 0 deletions

View File

@ -38,6 +38,10 @@ public class Schlenglein <T>{
return this.maxSize;
}
public boolean isEmpty(){
return this.size()<=0;
}
public void attemptDynamicResize(){
if (numberOfElements < 0.2*maxSize){
// Kleiner Machen
@ -62,4 +66,13 @@ public class Schlenglein <T>{
System.arraycopy(temp, 0, myArray, 0, maxSize / 2);
}
}
public void printAll(){
System.out.print("[");
for (int i = 0; i < this.size()-1; i++) {
System.out.print(this.myArray[i] + ", ");
}
System.out.println(this.myArray[this.size()-1] + "]");
}
}

12
src/part4/aufg1/Demo.java Normal file
View File

@ -0,0 +1,12 @@
package part4.aufg1;
public class Demo {
public static void main(String[] args) {
ThreadedCounter tc = new ThreadedCounter(-1,1);
ThreadedCounter tC = new ThreadedCounter(4,2);
tc.start();
tC.start();
}
}

View File

@ -0,0 +1,30 @@
package part4.aufg1;
public class ThreadedCounter extends Thread {
private final int threadNumber;
private final int increment;
static int sharedInt = 0;
static int iterations = 0;
public ThreadedCounter(int increment, int threadNumber) {
this.increment = increment;
this.threadNumber = threadNumber;
}
public void run() {
while (sharedInt < 1000){
synchronized (getClass()) {
iterations++;
sharedInt += increment;
System.out.println("Thread " + threadNumber + " made the shared Int: " + sharedInt);
try {
sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
System.out.println("it took " + iterations + " iterations");
}
}

28
src/part4/aufg2/Demo.java Normal file
View File

@ -0,0 +1,28 @@
package part4.aufg2;
/*
In einem System arbeiten eine Reihe von Benutzer, die von Zeit zu Zeit
Druckaufträge eines gewissen Umfangs erzeugen.
Diese werden in einer Druckerwarteschlange verwaltet.
Zwei Drucker stehen zur Verfügung und arbeiten die Aufträge ab.
Modellieren Sie das Problem als ErzeugerVerbraucherProblem und realisieren Sie es in Java.
*/
public class Demo {
public static void main(String[] args) {
int numUsers = 7;
int numPrinter = 4;
for (int i = 1; i <= numUsers; i++) {
User user = new User(i);
user.start();
}
for (int i = 1; i <= numPrinter ; i++) {
Printer printer = new Printer(i);
printer.start();
}
}
}

View File

@ -0,0 +1,31 @@
package part4.aufg2;
public class Printer extends Thread {
private int threadNumber;
public Printer(int threadNumber){
this.threadNumber = threadNumber;
}
public void run() {
while (true) {
try {
synchronized (Puwwer.q) {
if (Puwwer.q.isEmpty()){
try {
Puwwer.q.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
System.out.println("Printer " + this.threadNumber + " prints " + Puwwer.q.get());
}
} catch (NullPointerException e){}
try {
sleep(100);
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
}
}
}

View File

@ -0,0 +1,7 @@
package part4.aufg2;
import part3.aufg2.Schlenglein;
public class Puwwer {
static Schlenglein q = new Schlenglein();
}

36
src/part4/aufg2/User.java Normal file
View File

@ -0,0 +1,36 @@
package part4.aufg2;
public class User extends Thread {
private int threadNumber;
public User(int threadNumber) {
this.threadNumber = threadNumber;
}
public synchronized void makeRequest() {
synchronized (Puwwer.q) {
int randomInt = (int) (Math.random() * ((int) (Math.random() * 1000)));
String randomIntStr = this.threadNumber + " " + randomInt;
Puwwer.q.add(randomIntStr);
System.out.println("User " + threadNumber + " added " + randomIntStr);
try {
Puwwer.q.notify();
} catch (IllegalMonitorStateException e) {
throw new RuntimeException(e);
}
}
}
public void run() {
while (true) {
makeRequest();
try {
sleep(3000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}