Dining Philosophers project
commit
b943a4d3ad
|
@ -0,0 +1,3 @@
|
|||
/target/
|
||||
/.classpath
|
||||
/.project
|
|
@ -0,0 +1,61 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>pp</groupId>
|
||||
<artifactId>pp.A1-CondPhilosophers</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<properties>
|
||||
<exec.mainClass>pp.PhilosopherExperiment</exec.mainClass>
|
||||
<maven.compiler.release>10</maven.compiler.release>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency><!-- für Unit-Tests -->
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>5.10.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency><!-- für Lombok -->
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.30</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency><!-- für net.jcip Annotationen -->
|
||||
<groupId>net.jcip</groupId>
|
||||
<artifactId>jcip-annotations</artifactId>
|
||||
<version>1.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin><!-- für Unit-Tests [mvn test] -->
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>3.0.0</version>
|
||||
</plugin>
|
||||
<plugin><!-- [mvn compile] -->
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.9.0</version>
|
||||
</plugin>
|
||||
<plugin><!-- [mvn exec:java] -->
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>exec-maven-plugin</artifactId>
|
||||
<version>3.1.0</version>
|
||||
</plugin>
|
||||
<plugin><!-- [mvn javadoc:javadoc] -->
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<version>3.5.0</version>
|
||||
<configuration>
|
||||
<show>private</show>
|
||||
<locale>en_US</locale>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -0,0 +1,30 @@
|
|||
package pp;
|
||||
|
||||
import java.util.concurrent.locks.Lock;
|
||||
|
||||
public interface IPhilosopher {
|
||||
|
||||
void run();
|
||||
|
||||
void setLeft(IPhilosopher left);
|
||||
|
||||
void setRight(IPhilosopher right);
|
||||
|
||||
void setSeat(int seat);
|
||||
|
||||
void setTable(Lock table);
|
||||
|
||||
void start();
|
||||
|
||||
void stopPhilosopher();
|
||||
|
||||
default void log(int seat, String message) {
|
||||
synchronized (Philosopher.class) {
|
||||
for (var i = 1; i <= seat; i++) {
|
||||
System.out.print(" ");
|
||||
}
|
||||
System.out.println("P" + seat + ": " + message);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,138 @@
|
|||
package pp;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.locks.Condition;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
public class Philosopher extends Thread implements IPhilosopher {
|
||||
private int seat;
|
||||
private Philosopher rechts;
|
||||
private Philosopher links;
|
||||
private Lock table;
|
||||
private Condition canEat;
|
||||
private volatile boolean stopped;
|
||||
private volatile boolean eating;
|
||||
private final Random random;
|
||||
|
||||
public Philosopher() {
|
||||
this.random = new Random();
|
||||
this.seat = 0;
|
||||
this.stopped = false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
while (!this.stopped) {
|
||||
think();
|
||||
eat();
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
log(seat,"beendet");
|
||||
}
|
||||
|
||||
private void eat() throws InterruptedException {
|
||||
this.table.lock();
|
||||
try {
|
||||
|
||||
while (this.links.eating || this.rechts.eating) {
|
||||
log(seat,"warten!");
|
||||
this.canEat.await();
|
||||
}
|
||||
this.eating = true;
|
||||
log(seat, " : isst gerade!");
|
||||
} finally {
|
||||
table.unlock();
|
||||
}
|
||||
Thread.sleep(this.random.nextInt(PhilosopherExperiment.MAX_EATING_DURATION_MS));
|
||||
}
|
||||
|
||||
private void think() throws InterruptedException {
|
||||
this.table.lock();
|
||||
|
||||
try {
|
||||
if (this.eating)
|
||||
this.eating = false;
|
||||
this.links.canEat.signal();
|
||||
this.rechts.canEat.signal();
|
||||
log(seat,"denken");
|
||||
|
||||
} finally {
|
||||
this.table.unlock();
|
||||
}
|
||||
|
||||
Thread.sleep(random.nextInt(PhilosopherExperiment.MAX_THINKING_DURATION_MS));
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLeft(IPhilosopher left) {
|
||||
// TODO Auto-generated method stub
|
||||
// Cast auf Philosopher erforderlich
|
||||
this.links = (Philosopher) left;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRight(IPhilosopher right) {
|
||||
// TODO Auto-generated method stub
|
||||
// Cast auf Philosopher erforderlich
|
||||
this.rechts = (Philosopher) right;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSeat(int seat) {
|
||||
this.seat = seat;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTable(Lock table) {
|
||||
// TODO Auto-generated method stub
|
||||
this.table = table;
|
||||
canEat = this.table.newCondition();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stopPhilosopher() {
|
||||
this.stopped = true;
|
||||
interrupt();
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
int numPhilosophers = 5;
|
||||
Lock table = new ReentrantLock();
|
||||
Philosopher[] philosophers = new Philosopher[numPhilosophers];
|
||||
|
||||
for (int i = 0; i < numPhilosophers; i++) {
|
||||
philosophers[i] = new Philosopher();
|
||||
philosophers[i].setSeat(i);
|
||||
philosophers[i].setTable(table);
|
||||
}
|
||||
|
||||
for (int i = 0; i < numPhilosophers; i++) {
|
||||
philosophers[i].setLeft(philosophers[(i + numPhilosophers - 1) % numPhilosophers]);
|
||||
philosophers[i].setRight(philosophers[(i + 1) % numPhilosophers]);
|
||||
}
|
||||
|
||||
for (Philosopher p : philosophers) {
|
||||
p.start();
|
||||
}
|
||||
|
||||
Thread.sleep(10_000); // Laufzeit: 10 Sekunden
|
||||
|
||||
for (Philosopher p : philosophers) {
|
||||
p.stopPhilosopher();
|
||||
}
|
||||
|
||||
for (Philosopher p : philosophers) {
|
||||
p.join();
|
||||
}
|
||||
|
||||
System.out.println("Experiment beendet!");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package pp;
|
||||
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
public class PhilosopherExperiment {
|
||||
static final int MAX_THINKING_DURATION_MS = 3000;
|
||||
static final int MAX_EATING_DURATION_MS = 3000;
|
||||
static final int MAX_TAKING_TIME_MS = 100;
|
||||
static final int PHILOSOPHER_NUM = 5;
|
||||
static final int EXP_DURATION_MS = 20000;
|
||||
static IPhilosopher[] philosophers = new Philosopher[PHILOSOPHER_NUM];
|
||||
|
||||
public static void main(String... args) throws InterruptedException {
|
||||
var table = new ReentrantLock();
|
||||
for (var i = 0; i < PHILOSOPHER_NUM; i++) {
|
||||
philosophers[i] = new Philosopher();
|
||||
philosophers[i].setTable(table);
|
||||
philosophers[i].setSeat(i);
|
||||
}
|
||||
philosophers[0].setLeft(philosophers[PHILOSOPHER_NUM - 1]);
|
||||
philosophers[0].setRight(philosophers[1]);
|
||||
for (var i = 1; i < (PHILOSOPHER_NUM - 1); i++) {
|
||||
philosophers[i].setLeft(philosophers[i - 1]);
|
||||
philosophers[i].setRight(philosophers[i + 1]);
|
||||
}
|
||||
philosophers[PHILOSOPHER_NUM - 1]
|
||||
.setLeft(philosophers[PHILOSOPHER_NUM - 2]);
|
||||
philosophers[PHILOSOPHER_NUM - 1].setRight(philosophers[0]);
|
||||
for (var i = 0; i < PHILOSOPHER_NUM; i++) {
|
||||
philosophers[i].start();
|
||||
}
|
||||
Thread.sleep(EXP_DURATION_MS);
|
||||
for (var i = 0; i < PHILOSOPHER_NUM; i++) {
|
||||
philosophers[i].stopPhilosopher();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
# Dining Philosophers (Java)
|
||||
|
||||
## Projektbeschreibung
|
||||
Dieses Projekt implementiert das klassische **Dining-Philosophers-Problem** in Java.
|
||||
Es zeigt, wie mehrere Threads gleichzeitig arbeiten und dabei über **Locks und Conditions** synchronisiert werden, um Deadlocks zu vermeiden.
|
||||
|
||||
## Projektstruktur
|
||||
- `IPhilosopher.java` – Interface für Philosophen
|
||||
- `Philosopher.java` – Implementierung eines Philosophen als Thread
|
||||
- `PhilosopherExperiment.java` – Startklasse für die Simulation
|
||||
|
||||
## Technologien
|
||||
- Java (Threads, Locks, Conditions)
|
||||
- Maven (Build- und Projektverwaltung)
|
||||
|
||||
## Ausführen
|
||||
1. Mit Maven bauen:
|
||||
```bash
|
||||
mvn clean install
|
Loading…
Reference in New Issue