Aufgabe 1

main
Yan Wittmann 2022-10-23 11:01:42 +02:00
parent d7f02ce8f9
commit 525a45d687
3 changed files with 101 additions and 14 deletions

View File

@ -4,6 +4,7 @@ import java.util.concurrent.locks.Lock;
public interface IPhilosopher { public interface IPhilosopher {
void run(); void run();
void setLeft(IPhilosopher left); void setLeft(IPhilosopher left);

View File

@ -1,36 +1,116 @@
package io.dama.ffi.parcoord.dining.cond; package io.dama.ffi.parcoord.dining.cond;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.Lock;
/**
* Bauen Sie die (synchronisierten) dinierenden Philosophen um, so dass Bedingungsvariablen benutzt werden:
* <p>
* - Jeder Philosoph ist ein Thread und besitzt einen Warteraum, in dem die Nachbarphilosophen darauf warten, dass er seine
* Stäbchen freigibt.
* - Der Name der Klasse muss io.dama.ffi.parcoord.dining.cond.Philosopher sein.
* - Philosoph implementiert das Interface io.dama.ffi.parcoord.dining.cond.IPhilosopher
* - Jeder Philosoph hat je eine Referenz auf seinen linken und rechten Nachbarn. setLeft(IPhilosopher left); setRight(
* IPhilosopher right);
* - Der Tisch wird durch ein ReentrantLock-Objekt (Interface Lock) repräsentiert. Alle Philosophen müssen den Tisch
* verwenden, wenn sie beginnen zu essen. Eine Referenz auf den Tisch kann mit dem Setter übergeben werden: setTable(Lock
* table);
* - Möchte ein Philosoph beginnen zu essen, prüft er, ob sein linker Nachbar oder sein rechter Nachbar isst; er erwartet
* den Moment, in dem beide nicht essen. Dann beginnt er zu essen.
* - Wenn ein Philosoph denkt, isst er nicht und signalisiert jeweils seinem linken Nachbarn und seinem rechten Nachbarn,
* dass er nicht isst.
*/
public class Philosopher extends Thread implements IPhilosopher { public class Philosopher extends Thread implements IPhilosopher {
private Philosopher left;
private Philosopher right;
private int seat;
private Lock table;
private Condition condition;
private boolean stop = false;
private boolean isEating = false;
private void log(String message) {
synchronized (Philosopher.class) {
for (var i = 1; i <= this.seat; i++) {
System.out.print(" ".repeat(40));
}
System.out.println(message);
}
}
@Override @Override
public void setLeft(IPhilosopher left) { public void setLeft(IPhilosopher left) {
// TODO Auto-generated method stub this.left = (Philosopher) left;
// Cast auf Philosopher erforderlich
} }
@Override @Override
public void setRight(IPhilosopher right) { public void setRight(IPhilosopher right) {
// TODO Auto-generated method stub this.right = (Philosopher) right;
// Cast auf Philosopher erforderlich
} }
@Override @Override
public void setSeat(int seat) { public void setSeat(int seat) {
// TODO Auto-generated method stub this.seat = seat;
} }
@Override @Override
public void setTable(Lock table) { public void setTable(Lock table) {
// TODO Auto-generated method stub this.table = table;
this.condition = table.newCondition();
} }
@Override @Override
public void stopPhilosopher() { public void stopPhilosopher() {
// TODO Auto-generated method stub log("Philosopher " + seat + " is stopping");
this.stop = true;
}
@Override
public void run() {
while (!this.stop) {
this.sleepRandomDuration(PhilosopherExperiment.MAX_THINKING_DURATION_MS);
try {
this.table.lock();
while (this.left.isEating || this.right.isEating) {
log("Philosopher " + seat + " is waiting for his neighbors to finish eating");
this.left.condition.await();
this.right.condition.await();
}
this.isEating = true;
log("Philosopher " + seat + " is taking chopsticks");
this.sleepRandomDuration(PhilosopherExperiment.MAX_TAKING_TIME_MS);
log("Philosopher " + seat + " is eating");
this.sleepRandomDuration(PhilosopherExperiment.MAX_EATING_DURATION_MS);
log("Philosopher " + seat + " is done eating");
this.isEating = false;
} catch (InterruptedException e) {
log("Philosopher " + seat + " was interrupted");
throw new RuntimeException(e);
} finally {
this.condition.signalAll();
this.table.unlock();
}
}
log("Philosopher " + seat + " has stopped");
}
private void sleepRandomDuration(int max) {
try {
final int duration = (int) (Math.random() * max);
log("Philosopher " + seat + " is occupied for " + duration + "ms");
Thread.sleep(duration);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
} }
} }

View File

@ -3,33 +3,39 @@ package io.dama.ffi.parcoord.dining.cond;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
public class PhilosopherExperiment { public class PhilosopherExperiment {
static final int MAX_THINKING_DURATION_MS = 3000;
static final int MAX_EATING_DURATION_MS = 3000; static final int MAX_THINKING_DURATION_MS = 3000; // 3000
static final int MAX_TAKING_TIME_MS = 100; static final int MAX_EATING_DURATION_MS = 3000; // 3000
static final int MAX_TAKING_TIME_MS = 100; // 100
static final int PHILOSOPHER_NUM = 5; static final int PHILOSOPHER_NUM = 5;
static final int EXP_DURATION_MS = 20000; static final int EXP_DURATION_MS = 20000;
static IPhilosopher[] philosophers = new Philosopher[PHILOSOPHER_NUM]; static IPhilosopher[] philosophers = new Philosopher[PHILOSOPHER_NUM];
public static void main(String... args) throws InterruptedException { public static void main(String... args) throws InterruptedException {
var table = new ReentrantLock(); var table = new ReentrantLock();
for (var i = 0; i < PHILOSOPHER_NUM; i++) { for (var i = 0; i < PHILOSOPHER_NUM; i++) {
philosophers[i] = new Philosopher(); philosophers[i] = new Philosopher();
philosophers[i].setTable(table); philosophers[i].setTable(table);
philosophers[i].setSeat(i); philosophers[i].setSeat(i);
} }
philosophers[0].setLeft(philosophers[PHILOSOPHER_NUM - 1]); philosophers[0].setLeft(philosophers[PHILOSOPHER_NUM - 1]);
philosophers[0].setRight(philosophers[1]); philosophers[0].setRight(philosophers[1]);
for (var i = 1; i < (PHILOSOPHER_NUM - 1); i++) { for (var i = 1; i < (PHILOSOPHER_NUM - 1); i++) {
philosophers[i].setLeft(philosophers[i - 1]); philosophers[i].setLeft(philosophers[i - 1]);
philosophers[i].setRight(philosophers[i + 1]); philosophers[i].setRight(philosophers[i + 1]);
} }
philosophers[PHILOSOPHER_NUM - 1] philosophers[PHILOSOPHER_NUM - 1].setLeft(philosophers[PHILOSOPHER_NUM - 2]);
.setLeft(philosophers[PHILOSOPHER_NUM - 2]);
philosophers[PHILOSOPHER_NUM - 1].setRight(philosophers[0]); philosophers[PHILOSOPHER_NUM - 1].setRight(philosophers[0]);
for (var i = 0; i < PHILOSOPHER_NUM; i++) { for (var i = 0; i < PHILOSOPHER_NUM; i++) {
philosophers[i].start(); philosophers[i].start();
} }
Thread.sleep(EXP_DURATION_MS); Thread.sleep(EXP_DURATION_MS);
for (var i = 0; i < PHILOSOPHER_NUM; i++) { for (var i = 0; i < PHILOSOPHER_NUM; i++) {
philosophers[i].stopPhilosopher(); philosophers[i].stopPhilosopher();
} }