Erweiterung PU1 und weiteres
parent
8d5e2a3232
commit
173458e5f2
33
.classpath
33
.classpath
|
@ -1,37 +1,14 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<classpath>
|
<classpath>
|
||||||
<classpathentry kind="src" output="target/classes" path="src/main/java">
|
<classpathentry excluding="main/java/|main/resources/|test/java/|test/resources/" kind="src" path="src"/>
|
||||||
|
<classpathentry kind="src" path="eigenes"/>
|
||||||
|
<classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
|
||||||
<attributes>
|
<attributes>
|
||||||
<attribute name="optional" value="true"/>
|
<attribute name="module" value="true"/>
|
||||||
<attribute name="maven.pomderived" value="true"/>
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
</attributes>
|
</attributes>
|
||||||
</classpathentry>
|
</classpathentry>
|
||||||
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
|
<classpathentry exported="true" kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
|
||||||
<attributes>
|
|
||||||
<attribute name="maven.pomderived" value="true"/>
|
|
||||||
<attribute name="optional" value="true"/>
|
|
||||||
</attributes>
|
|
||||||
</classpathentry>
|
|
||||||
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
|
|
||||||
<attributes>
|
|
||||||
<attribute name="optional" value="true"/>
|
|
||||||
<attribute name="maven.pomderived" value="true"/>
|
|
||||||
<attribute name="test" value="true"/>
|
|
||||||
</attributes>
|
|
||||||
</classpathentry>
|
|
||||||
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
|
|
||||||
<attributes>
|
|
||||||
<attribute name="maven.pomderived" value="true"/>
|
|
||||||
<attribute name="test" value="true"/>
|
|
||||||
<attribute name="optional" value="true"/>
|
|
||||||
</attributes>
|
|
||||||
</classpathentry>
|
|
||||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
|
|
||||||
<attributes>
|
|
||||||
<attribute name="maven.pomderived" value="true"/>
|
|
||||||
</attributes>
|
|
||||||
</classpathentry>
|
|
||||||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
|
|
||||||
<attributes>
|
<attributes>
|
||||||
<attribute name="maven.pomderived" value="true"/>
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
</attributes>
|
</attributes>
|
||||||
|
|
|
@ -0,0 +1,55 @@
|
||||||
|
package philosopher2;
|
||||||
|
|
||||||
|
import src.Chopstick;
|
||||||
|
|
||||||
|
public class Philosopher implements Runnable {
|
||||||
|
private final Chopstick leftChopstick;
|
||||||
|
private final Chopstick rightChopstick;
|
||||||
|
private final boolean isRightHanded; // Asymmetry flag
|
||||||
|
|
||||||
|
public Philosopher(Chopstick leftChopstick, Chopstick rightChopstick, boolean isRightHanded) {
|
||||||
|
this.leftChopstick = leftChopstick;
|
||||||
|
this.rightChopstick = rightChopstick;
|
||||||
|
this.isRightHanded = isRightHanded;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
while (true) {
|
||||||
|
think();
|
||||||
|
if (isRightHanded) {
|
||||||
|
synchronized (rightChopstick) {
|
||||||
|
rightChopstick.pickUp();
|
||||||
|
synchronized (leftChopstick) {
|
||||||
|
leftChopstick.pickUp();
|
||||||
|
eat();
|
||||||
|
leftChopstick.putDown();
|
||||||
|
}
|
||||||
|
rightChopstick.putDown();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
synchronized (leftChopstick) {
|
||||||
|
leftChopstick.pickUp();
|
||||||
|
synchronized (rightChopstick) {
|
||||||
|
rightChopstick.pickUp();
|
||||||
|
eat();
|
||||||
|
rightChopstick.putDown();
|
||||||
|
}
|
||||||
|
leftChopstick.putDown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void think() {
|
||||||
|
System.out.println(Thread.currentThread().getName() + " is thinking.");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void eat() {
|
||||||
|
System.out.println(Thread.currentThread().getName() + " is eating.");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,56 @@
|
||||||
|
package philosopher3;
|
||||||
|
|
||||||
|
import src.Chopstick;
|
||||||
|
|
||||||
|
public class Philosopher implements Runnable {
|
||||||
|
private final Chopstick leftChopstick;
|
||||||
|
private final Chopstick rightChopstick;
|
||||||
|
|
||||||
|
public Philosopher(Chopstick leftChopstick, Chopstick rightChopstick) {
|
||||||
|
this.leftChopstick = leftChopstick;
|
||||||
|
this.rightChopstick = rightChopstick;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
while (true) {
|
||||||
|
think();
|
||||||
|
if (tryToEat()) {
|
||||||
|
eat();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void think() {
|
||||||
|
System.out.println(Thread.currentThread().getName() + " is thinking.");
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean tryToEat() throws InterruptedException {
|
||||||
|
synchronized (leftChopstick) {
|
||||||
|
leftChopstick.pickUp();
|
||||||
|
if (!tryPickUpRightChopstick()) {
|
||||||
|
leftChopstick.putDown();
|
||||||
|
return false; // Couldn't get both chopsticks, return to thinking
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean tryPickUpRightChopstick() throws InterruptedException {
|
||||||
|
synchronized (rightChopstick) {
|
||||||
|
rightChopstick.pickUp();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void eat() throws InterruptedException {
|
||||||
|
System.out.println(Thread.currentThread().getName() + " is eating.");
|
||||||
|
rightChopstick.putDown();
|
||||||
|
leftChopstick.putDown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,19 @@
|
||||||
|
package src;
|
||||||
|
|
||||||
|
import java.util.concurrent.Semaphore;
|
||||||
|
|
||||||
|
public class Arbitrator {
|
||||||
|
private final Semaphore semaphore;
|
||||||
|
|
||||||
|
public Arbitrator(int maxPhilosophers) {
|
||||||
|
this.semaphore = new Semaphore(maxPhilosophers - 1); // Allow up to N-1 philosophers to eat
|
||||||
|
}
|
||||||
|
|
||||||
|
public void requestPermission(Philosopher philosopher) throws InterruptedException {
|
||||||
|
semaphore.acquire(); // Request permission to eat
|
||||||
|
}
|
||||||
|
|
||||||
|
public void releasePermission(Philosopher philosopher) {
|
||||||
|
semaphore.release(); // Release permission after eating
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,20 @@
|
||||||
|
package src;
|
||||||
|
|
||||||
|
public class Chopstick {
|
||||||
|
private boolean inUse = false;
|
||||||
|
|
||||||
|
// Ein Philosophen-Thread nimmt das Stäbchen
|
||||||
|
public synchronized void pickUp() throws InterruptedException {
|
||||||
|
while (inUse) {
|
||||||
|
wait(); // Warten, bis das Stäbchen verfügbar ist
|
||||||
|
}
|
||||||
|
inUse = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ein Philosophen-Thread legt das Stäbchen wieder zurück
|
||||||
|
public synchronized void putDown() {
|
||||||
|
inUse = false;
|
||||||
|
notifyAll(); // Andere wartende Philosophen benachrichtigen
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,43 @@
|
||||||
|
package src;
|
||||||
|
public class Philosopher implements Runnable {
|
||||||
|
private final Chopstick leftChopstick;
|
||||||
|
private final Chopstick rightChopstick;
|
||||||
|
private final Arbitrator arbitrator;
|
||||||
|
|
||||||
|
public Philosopher(Chopstick leftChopstick, Chopstick rightChopstick, Arbitrator arbitrator) {
|
||||||
|
this.leftChopstick = leftChopstick;
|
||||||
|
this.rightChopstick = rightChopstick;
|
||||||
|
this.arbitrator = arbitrator;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
while (true) {
|
||||||
|
think();
|
||||||
|
arbitrator.requestPermission(this); // Request permission from the arbitrator
|
||||||
|
synchronized (leftChopstick) {
|
||||||
|
leftChopstick.pickUp();
|
||||||
|
synchronized (rightChopstick) {
|
||||||
|
rightChopstick.pickUp();
|
||||||
|
eat();
|
||||||
|
rightChopstick.putDown();
|
||||||
|
}
|
||||||
|
leftChopstick.putDown();
|
||||||
|
}
|
||||||
|
arbitrator.releasePermission(this); // Release permission
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void think() {
|
||||||
|
System.out.println(Thread.currentThread().getName() + " is thinking.");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void eat() {
|
||||||
|
System.out.println(Thread.currentThread().getName() + " is eating.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue