Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 KB | None | 0 0
  1. using System;
  2.  
  3. using System.Threading;
  4.  
  5. namespace _5Filosofi
  6. {
  7. class HoldForchetta
  8. {
  9. private int importanza;
  10.  
  11. public int Importanza
  12. {
  13. get { return importanza; } set { importanza = value; }
  14. }
  15. public HoldForchetta(int imp)
  16. {
  17. Importanza = imp;
  18. }
  19. }
  20. class Filosofo
  21. {
  22. private HoldForchetta forchettaDx;
  23. private HoldForchetta forchettaSx;
  24. private int manoDx;
  25. private int manoSx;
  26. private Random randomSleepTime;
  27. private int importanza;
  28.  
  29. public int Importanza
  30. {
  31. get { return importanza; }
  32. set { importanza = value; }
  33. }
  34.  
  35. public Filosofo(HoldForchetta Dx, HoldForchetta Sx, Random random, int imp)
  36. {
  37. forchettaDx=Dx;forchettaSx=Sx;
  38. randomSleepTime = random;
  39. Importanza = imp;
  40. }
  41.  
  42. public void Filosofa()
  43. {
  44. while (true)
  45. {
  46. Thread.Sleep(randomSleepTime.Next(1, 3000));
  47. if (forchettaDx.Importanza < forchettaSx.Importanza)
  48. {
  49. Monitor.Enter(forchettaDx);
  50. Console.WriteLine("Filosofo {0} Prende forchetta Destra {1}", this.Importanza.ToString(), forchettaDx.Importanza.ToString());
  51. Monitor.Enter(forchettaSx);
  52. Console.WriteLine("Filosofo {0} Prende forchetta Sinistra {1}", this.Importanza.ToString(), forchettaSx.Importanza.ToString());
  53. }
  54. else
  55. {
  56. Monitor.Enter(forchettaSx);
  57. Console.WriteLine("Filosofo {0} Prende forchetta Sinistra {1}", this.Importanza.ToString(), forchettaSx.Importanza.ToString());
  58. Monitor.Enter(forchettaDx);
  59. Console.WriteLine("Filosofo {0} Prende forchetta Destra {1}", this.Importanza.ToString(), forchettaDx.Importanza.ToString());
  60. }
  61. Thread.Sleep(randomSleepTime.Next(1, 3000));
  62. Console.WriteLine("\nFilosofo {0} sta mangiando\n", this.Importanza.ToString());
  63.  
  64. if (forchettaDx.Importanza < forchettaSx.Importanza)
  65. {
  66. Monitor.Exit(forchettaSx);
  67. Console.WriteLine("Filosofo {0} Rilascia forchetta Sinistra {1}", this.Importanza.ToString(), forchettaSx.Importanza.ToString());
  68. Monitor.Exit(forchettaDx);
  69. Console.WriteLine("Filosofo {0} Rilascia forchetta Destra {1}", this.Importanza.ToString(), forchettaDx.Importanza.ToString());
  70. }
  71. else
  72. {
  73. Monitor.Exit(forchettaDx);
  74. Console.WriteLine("Filosofo {0} Rilascia forchetta Destra {1}", this.Importanza.ToString(), forchettaDx.Importanza.ToString());
  75. Monitor.Exit(forchettaSx);
  76. Console.WriteLine("Filosofo {0} Rilascia forchetta Sinistra {1}", this.Importanza.ToString(), forchettaSx.Importanza.ToString());
  77. }
  78.  
  79.  
  80. }
  81. }
  82.  
  83.  
  84. }
  85. class Program
  86. {
  87. static void Main(string[] args)
  88. {
  89. HoldForchetta[] holdForc = new HoldForchetta[5];
  90.  
  91. holdForc[0] = new HoldForchetta(1);
  92. holdForc[1] = new HoldForchetta(2);
  93. holdForc[2] = new HoldForchetta(3);
  94. holdForc[3] = new HoldForchetta(4);
  95. holdForc[4] = new HoldForchetta(5);
  96.  
  97. Random random = new Random();
  98.  
  99. Filosofo[] Filosofo = new Filosofo[5];
  100.  
  101. Filosofo[0] = new Filosofo(holdForc[1], holdForc[0], random, 1);
  102. Filosofo[1] = new Filosofo(holdForc[2], holdForc[1], random, 2);
  103. Filosofo[2] = new Filosofo(holdForc[3], holdForc[2], random, 3);
  104. Filosofo[3] = new Filosofo(holdForc[4], holdForc[3], random, 4);
  105. Filosofo[4] = new Filosofo(holdForc[0], holdForc[4], random, 5);
  106.  
  107. Thread FilosofoThread0 = new Thread(new ThreadStart(Filosofo[0].Filosofa));
  108. Thread FilosofoThread1 = new Thread(new ThreadStart(Filosofo[1].Filosofa));
  109. Thread FilosofoThread2 = new Thread(new ThreadStart(Filosofo[2].Filosofa));
  110. Thread FilosofoThread3 = new Thread(new ThreadStart(Filosofo[3].Filosofa));
  111. Thread FilosofoThread4 = new Thread(new ThreadStart(Filosofo[4].Filosofa));
  112. FilosofoThread0.Name = "Filosofo 1";
  113. FilosofoThread1.Name = "Filosofo 2";
  114. FilosofoThread2.Name = "Filosofo 3";
  115. FilosofoThread3.Name = "Filosofo 4";
  116. FilosofoThread4.Name = "Filosofo 5";
  117. FilosofoThread0.Start();
  118. FilosofoThread1.Start();
  119. FilosofoThread2.Start();
  120. FilosofoThread3.Start();
  121. FilosofoThread4.Start();
  122. }
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement