Added given files

main
2wenty1ne 2024-10-26 17:49:49 +02:00
commit 2f5e5dbe33
4 changed files with 164 additions and 0 deletions

View File

@ -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>

View File

@ -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);
}
}
}

View File

@ -0,0 +1,36 @@
package pp;
import java.util.concurrent.locks.Lock;
public class Philosopher extends Thread implements IPhilosopher {
private int seat;
@Override
public void setLeft(IPhilosopher left) {
// TODO Auto-generated method stub
// Cast auf Philosopher erforderlich
}
@Override
public void setRight(IPhilosopher right) {
// TODO Auto-generated method stub
// Cast auf Philosopher erforderlich
}
@Override
public void setSeat(int seat) {
this.seat = seat;
}
@Override
public void setTable(Lock table) {
// TODO Auto-generated method stub
}
@Override
public void stopPhilosopher() {
// TODO Auto-generated method stub
}
}

View File

@ -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();
}
}
}