123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- import java.util.concurrent.locks.Lock;
- import java.util.concurrent.locks.ReentrantLock;
-
- public class DiningPhilosophers {
-
- // Anzahl der Philosophen
- private static final int NUM_PHILOSOPHERS = 5;
-
- public static void main(String[] args) {
- Philosopher[] philosophers = new Philosopher[NUM_PHILOSOPHERS];
- Fork[] forks = new Fork[NUM_PHILOSOPHERS];
-
- // Initialisiere Gabeln
- for (int i = 0; i < NUM_PHILOSOPHERS; i++) {
- forks[i] = new Fork(i);
- }
-
- // Initialisiere Philosophen
- for (int i = 0; i < NUM_PHILOSOPHERS; i++) {
- Fork leftFork = forks[i];
- Fork rightFork = forks[(i + 1) % NUM_PHILOSOPHERS];
-
- // Um Deadlock zu vermeiden, greift der letzte Philosoph zuerst zur rechten Gabel.
- if (i == NUM_PHILOSOPHERS - 1) {
- philosophers[i] = new Philosopher(i, rightFork, leftFork);
- } else {
- philosophers[i] = new Philosopher(i, leftFork, rightFork);
- }
-
- new Thread(philosophers[i], "Philosopher-" + i).start();
- }
- }
- }
-
- class Fork {
-
- private final int id;
- private boolean inUse;
-
- // Konstruktor
- public Fork(int id) {
- this.id = id;
- this.inUse = false;
- }
-
- // Liefert die ID der Gabel zurück
- public int getId() {
- return id;
- }
-
- // Gabel aufnehmen
- public synchronized boolean take() {
- if (!inUse) {
- inUse = true; // Gabel ist jetzt in Benutzung
- try {
- Thread.sleep(1000); // 1 Sekunde warten, um die Gabel aufzunehmen
- } catch (InterruptedException e) {
- Thread.currentThread().interrupt();
- }
- return true;
- }
- return false; // Gabel ist bereits in Benutzung
- }
-
- // Gabel freigeben
- public synchronized void put() {
- inUse = false; // Gabel freigeben
- }
-
- @Override
- public String toString() {
- return "Fork-" + id;
- }
- }
-
-
- class Philosopher implements Runnable {
- private final int id;
- private final Fork leftFork;
- private final Fork rightFork;
-
- public Philosopher(int id, Fork leftFork, Fork rightFork) {
- this.id = id;
- this.leftFork = leftFork;
- this.rightFork = rightFork;
- }
-
- @Override
- public void run() {
- try {
-
- // Linke Gabel aufnehmen
- System.out.println("Philosopher-" + id + " is trying to get " + leftFork);
- while (!leftFork.take()) {
- Thread.yield(); // Busy waiting entschärfen
- }
- System.out.println("Philosopher-" + id + " got " + leftFork);
-
- // Rechte Gabel aufnehmen
- System.out.println("Philosopher-" + id + " is trying to get " + rightFork);
- while (!rightFork.take()) {
- Thread.yield(); // Busy waiting entschärfen
- }
- System.out.println("Philosopher-" + id + " got " + rightFork);
-
- // Philosoph isst
- System.out.println("Philosopher-" + id + " is eating.");
- Thread.sleep(2000); // 2 Sekunden essen
-
- // Gabeln freigeben
- leftFork.put();
- System.out.println("Philosopher-" + id + " put down " + leftFork);
- rightFork.put();
- System.out.println("Philosopher-" + id + " put down " + rightFork);
-
- // Philosoph beendet das Essen
- System.out.println("Philosopher-" + id + " finished eating.");
- } catch (InterruptedException e) {
- Thread.currentThread().interrupt();
- System.out.println("Philosopher-" + id + " was interrupted.");
- }
- }
- }
-
-
|