Initial commit
commit
8a4ace5d1e
|
|
@ -0,0 +1,10 @@
|
|||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Ignored default folder with query files
|
||||
/queries/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="CompilerConfiguration">
|
||||
<annotationProcessing>
|
||||
<profile name="Maven default annotation processors profile" enabled="true">
|
||||
<sourceOutputDir name="target/generated-sources/annotations" />
|
||||
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
|
||||
<outputRelativeToContentRoot value="true" />
|
||||
<module name="pp.A1-CondPhilosophers" />
|
||||
</profile>
|
||||
</annotationProcessing>
|
||||
</component>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Encoding">
|
||||
<file url="file://$PROJECT_DIR$/pp.A1-CondPhilosophers/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/pp.A1-CondPhilosophers/src/main/resources" charset="UTF-8" />
|
||||
</component>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="RemoteRepositoriesConfiguration">
|
||||
<remote-repository>
|
||||
<option name="id" value="central" />
|
||||
<option name="name" value="Central Repository" />
|
||||
<option name="url" value="https://repo.maven.apache.org/maven2" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="central" />
|
||||
<option name="name" value="Maven Central repository" />
|
||||
<option name="url" value="https://repo1.maven.org/maven2" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="jboss.community" />
|
||||
<option name="name" value="JBoss Community repository" />
|
||||
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
|
||||
</remote-repository>
|
||||
</component>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="KubernetesApiProvider"><![CDATA[{}]]></component>
|
||||
<component name="MavenProjectsManager">
|
||||
<option name="originalFiles">
|
||||
<list>
|
||||
<option value="$PROJECT_DIR$/pp.A1-CondPhilosophers/pom.xml" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_25" default="true" project-jdk-name="temurin-25 (2)" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/pp.A1-CondPhilosophers.iml" filepath="$PROJECT_DIR$/.idea/pp.A1-CondPhilosophers.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module version="4">
|
||||
<component name="AdditionalModuleElements">
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
</component>
|
||||
</module>
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</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.42</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,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
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue