Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3. import java.util.concurrent.Semaphore;
  4.  
  5. public class Philosophe extends Thread {
  6.  
  7. static int nb;
  8. Fourchette f1;
  9. Fourchette f2;
  10. Table t;
  11. int id;
  12.  
  13. public Philosophe(Fourchette f1, Fourchette f2, Table t) {
  14. this.f1 = f1;
  15. this.f2 = f2;
  16. this.id = nb++;
  17. this.t = t;
  18. }
  19.  
  20. public void reflechir() {
  21. while (!f1.tryAcquire()) {
  22. try {
  23. Thread.sleep(100);
  24. } catch (InterruptedException e) {
  25. e.printStackTrace();
  26. }
  27. }
  28. while (!f2.tryAcquire()) {
  29. try {
  30. Thread.sleep(100);
  31. } catch (InterruptedException e) {
  32. e.printStackTrace();
  33. }
  34. }
  35. }
  36.  
  37. public void manger() {
  38. try {
  39. if (!t.manger()) {
  40. System.out.println("Plus d'assietes !");
  41. this.interrupt();
  42. } else {
  43. Thread.sleep(500);
  44. f1.release();
  45. f2.release();
  46. }
  47.  
  48. } catch (InterruptedException e) {
  49. e.printStackTrace();
  50. }
  51. }
  52.  
  53. public void dormir() {
  54. try {
  55. Thread.sleep((long) (Math.random() * 5000));
  56. } catch (InterruptedException e) {
  57. }
  58. }
  59.  
  60. public Fourchette getFourchette1() {
  61. return f1;
  62. }
  63.  
  64. public Fourchette getFourchette2() {
  65. return f2;
  66. }
  67.  
  68. public void run() {
  69. while (true) {
  70. System.out.println("P" + id + ": reflechi");
  71. reflechir();
  72. System.out.println("P" + id + ": mange");
  73. manger();
  74. System.out.println("P" + id + ": dors");
  75. dormir();
  76. }
  77. }
  78.  
  79. public static void main(String[] a) {
  80. int nbAssietes = 500;
  81.  
  82. int nbPhilosophes = 0;
  83.  
  84. if (nbPhilosophes < 2) {
  85. do {
  86. Scanner sc = new Scanner(System.in);
  87.  
  88. try {
  89. nbPhilosophes = Integer.parseInt(sc.nextLine());
  90. } catch (Exception e) {
  91. }
  92. } while (nbPhilosophes < 2);
  93. }
  94.  
  95. if (nbAssietes < nbPhilosophes) {
  96. do {
  97. Scanner sc = new Scanner(System.in);
  98.  
  99. try {
  100. nbAssietes = Integer.parseInt(sc.nextLine());
  101. } catch (Exception e) {
  102. }
  103. } while (nbAssietes < nbPhilosophes);
  104. }
  105.  
  106. Table t = new Table(nbAssietes);
  107.  
  108. Fourchette f1 = new Fourchette();
  109. Fourchette fo = f1;
  110. Fourchette f2 = new Fourchette();
  111.  
  112. ArrayList<Philosophe> philos = new ArrayList<Philosophe>();
  113.  
  114. for (int i = 0; i < nbPhilosophes; i++) {
  115. f1 = f2;
  116.  
  117. if (i == nbPhilosophes - 1)
  118. f2 = fo;
  119. else
  120. f2 = new Fourchette();
  121.  
  122. Philosophe p = new Philosophe(f1, f2, t);
  123. philos.add(p);
  124. philos.get(i).start();
  125. }
  126.  
  127. }
  128. }
  129.  
  130. class Fourchette extends Semaphore {
  131. public Fourchette() {
  132. super(1, true);
  133. }
  134. }
  135.  
  136. class Table {
  137. int nbAssietes;
  138.  
  139. public Table(int assietes) {
  140. this.nbAssietes = assietes;
  141. }
  142.  
  143. public boolean manger() {
  144. if (this.nbAssietes <= 0)
  145. return false;
  146. this.nbAssietes--;
  147. return true;
  148. }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement