Repository zur Vorlesung Prog3A
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DiningPhilosophers.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import java.util.concurrent.locks.Lock;
  2. import java.util.concurrent.locks.ReentrantLock;
  3. public class DiningPhilosophers {
  4. // Anzahl der Philosophen
  5. private static final int NUM_PHILOSOPHERS = 5;
  6. public static void main(String[] args) {
  7. Philosopher[] philosophers = new Philosopher[NUM_PHILOSOPHERS];
  8. Fork[] forks = new Fork[NUM_PHILOSOPHERS];
  9. // Initialisiere Gabeln
  10. for (int i = 0; i < NUM_PHILOSOPHERS; i++) {
  11. forks[i] = new Fork(i);
  12. }
  13. // Initialisiere Philosophen
  14. for (int i = 0; i < NUM_PHILOSOPHERS; i++) {
  15. Fork leftFork = forks[i];
  16. Fork rightFork = forks[(i + 1) % NUM_PHILOSOPHERS];
  17. // Um Deadlock zu vermeiden, greift der letzte Philosoph zuerst zur rechten Gabel.
  18. if (i == NUM_PHILOSOPHERS - 1) {
  19. philosophers[i] = new Philosopher(i, rightFork, leftFork);
  20. } else {
  21. philosophers[i] = new Philosopher(i, leftFork, rightFork);
  22. }
  23. new Thread(philosophers[i], "Philosopher-" + i).start();
  24. }
  25. }
  26. }
  27. class Fork {
  28. private final int id;
  29. private boolean inUse;
  30. // Konstruktor
  31. public Fork(int id) {
  32. this.id = id;
  33. this.inUse = false;
  34. }
  35. // Liefert die ID der Gabel zurück
  36. public int getId() {
  37. return id;
  38. }
  39. // Gabel aufnehmen
  40. public synchronized boolean take() {
  41. if (!inUse) {
  42. inUse = true; // Gabel ist jetzt in Benutzung
  43. try {
  44. Thread.sleep(1000); // 1 Sekunde warten, um die Gabel aufzunehmen
  45. } catch (InterruptedException e) {
  46. Thread.currentThread().interrupt();
  47. }
  48. return true;
  49. }
  50. return false; // Gabel ist bereits in Benutzung
  51. }
  52. // Gabel freigeben
  53. public synchronized void put() {
  54. inUse = false; // Gabel freigeben
  55. }
  56. @Override
  57. public String toString() {
  58. return "Fork-" + id;
  59. }
  60. }
  61. class Philosopher implements Runnable {
  62. private final int id;
  63. private final Fork leftFork;
  64. private final Fork rightFork;
  65. public Philosopher(int id, Fork leftFork, Fork rightFork) {
  66. this.id = id;
  67. this.leftFork = leftFork;
  68. this.rightFork = rightFork;
  69. }
  70. @Override
  71. public void run() {
  72. try {
  73. // Linke Gabel aufnehmen
  74. System.out.println("Philosopher-" + id + " is trying to get " + leftFork);
  75. while (!leftFork.take()) {
  76. Thread.yield(); // Busy waiting entschärfen
  77. }
  78. System.out.println("Philosopher-" + id + " got " + leftFork);
  79. // Rechte Gabel aufnehmen
  80. System.out.println("Philosopher-" + id + " is trying to get " + rightFork);
  81. while (!rightFork.take()) {
  82. Thread.yield(); // Busy waiting entschärfen
  83. }
  84. System.out.println("Philosopher-" + id + " got " + rightFork);
  85. // Philosoph isst
  86. System.out.println("Philosopher-" + id + " is eating.");
  87. Thread.sleep(2000); // 2 Sekunden essen
  88. // Gabeln freigeben
  89. leftFork.put();
  90. System.out.println("Philosopher-" + id + " put down " + leftFork);
  91. rightFork.put();
  92. System.out.println("Philosopher-" + id + " put down " + rightFork);
  93. // Philosoph beendet das Essen
  94. System.out.println("Philosopher-" + id + " finished eating.");
  95. } catch (InterruptedException e) {
  96. Thread.currentThread().interrupt();
  97. System.out.println("Philosopher-" + id + " was interrupted.");
  98. }
  99. }
  100. }