Guten Tag,
das Problem ,dass ich momentan mit diesem Code habe, ist dass in dem Code irgendwo ein Read-Modify-Write Problem seien soll. Der Deadlock der momentan existiert ist gewollt ist nicht das Problem. Ich saß nun schon 2 Stunden vor diesem Code und hab überlegt wo der Fehler sein könnte, habe ihn aber nicht gefunden. Vielleicht hat eine von euch Glück
das Problem ,dass ich momentan mit diesem Code habe, ist dass in dem Code irgendwo ein Read-Modify-Write Problem seien soll. Der Deadlock der momentan existiert ist gewollt ist nicht das Problem. Ich saß nun schon 2 Stunden vor diesem Code und hab überlegt wo der Fehler sein könnte, habe ihn aber nicht gefunden. Vielleicht hat eine von euch Glück
Java:
package de.tuberlin.snet.prog2.ue04.philosophers;
import java.util.Random;
/**
* This class represents a philosopher. He is doing the following (in this order):
*
*/
public class Philosopher implements Runnable {
Chopstick leftChopstick;
Chopstick rightChopstick;
String name;
Random random = new Random();
public Philosopher(String name, Chopstick leftChopstick, Chopstick rightChopstick) {
this.name = name;
this.leftChopstick = leftChopstick;
this.rightChopstick = rightChopstick;
}
@Override
public void run() {
try {
while (true) {
leftChopstick.pickUp();
System.out.println(this + " picks up his left chopstick");
// to provoke a deadlock:
// Thread.yield();
rightChopstick.pickUp();
System.out.println(this + " picks up his right chopstick");
eat();
rightChopstick.putDown();
System.out.println(this + " puts down his right chopstick");
leftChopstick.putDown();
System.out.println(this + " puts down his left chopstick");
think();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void think() throws InterruptedException {
System.out.println(this + " is thinking");
Thread.sleep(random.nextInt(1000));
}
private void eat() throws InterruptedException {
System.out.println(this + " is eating");
Thread.sleep(random.nextInt(1000));
}
public String toString() {
return name;
}
}
Java:
package de.tuberlin.snet.prog2.ue04.philosophers;
/**
* This is an implementation of the dining philosophers problem.
* The given number of philosophers is created and started as threads.
*
*/
public class DiningPhilosophersProblem {
final int NUMBER_OF_PHILOSOPHERS = 5;
final String[] PHILOSOPHER_NAMES = {"Galilei", "Erasmus", "Dionysios", "Dewey", "Demokrit", "Cicero", "Bacon", "Aristoteles"};
Chopstick[] chopsticks;
Philosopher[] philosophers;
public DiningPhilosophersProblem() {
// prepare chopsticks
chopsticks = new Chopstick[NUMBER_OF_PHILOSOPHERS];
for (int i = 0; i < NUMBER_OF_PHILOSOPHERS; i++){
chopsticks[i] = new Chopstick();
}
// prepare philosophers
philosophers = new Philosopher[NUMBER_OF_PHILOSOPHERS];
for (int i = 0; i < NUMBER_OF_PHILOSOPHERS; i++){
String name;
if (i < PHILOSOPHER_NAMES.length)
name = PHILOSOPHER_NAMES[i] + "-" + i;
else
name = "Philosopher-i";
philosophers[i] = new Philosopher(name, chopsticks[i], chopsticks[(i+1) % NUMBER_OF_PHILOSOPHERS]);
}
// start philosophers
for (Philosopher phil: philosophers) {
new Thread(phil).start();
}
}
public static void main(String[] args) {
new DiningPhilosophersProblem();
}
}
Java:
package de.tuberlin.snet.prog2.ue04.philosophers;
/**
* Represents a chopstick used by the philosophers.
* If a philosopher holds the chopstick and another wants to
* pick it up he has to wait until the chopstick is free.
*
*
*
*/
public class Chopstick {
/** flag showing whether the chopstick is in use */
boolean inUse;
/**
* Waits until the chopstick is free and picks it up.
* @throws InterruptedException
*/
public void pickUp() throws InterruptedException {
while (isInUse()) {
Thread.sleep(100);
}
setInUse(true);
}
/**
* Puts the chopstick down so i can be used by another philosopher.
*/
public void putDown() {
setInUse(false);
}
/**
* Sets the {@link #inUse} variable.
* @param newValue
*/
private void setInUse(boolean newValue) {
inUse = newValue;
}
/**
* @return true, if the chopstick is in use, otherwise false
*/
private boolean isInUse() {
return inUse;
}
}