Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- namespace _5Filosofi
- {
- class HoldForchetta
- {
- private int importanza;
- public int Importanza
- {
- get { return importanza; } set { importanza = value; }
- }
- public HoldForchetta(int imp)
- {
- Importanza = imp;
- }
- }
- class Filosofo
- {
- private HoldForchetta forchettaDx;
- private HoldForchetta forchettaSx;
- private int manoDx;
- private int manoSx;
- private Random randomSleepTime;
- private int importanza;
- public int Importanza
- {
- get { return importanza; }
- set { importanza = value; }
- }
- public Filosofo(HoldForchetta Dx, HoldForchetta Sx, Random random, int imp)
- {
- forchettaDx=Dx;forchettaSx=Sx;
- randomSleepTime = random;
- Importanza = imp;
- }
- public void Filosofa()
- {
- while (true)
- {
- Thread.Sleep(randomSleepTime.Next(1, 3000));
- if (forchettaDx.Importanza < forchettaSx.Importanza)
- {
- Monitor.Enter(forchettaDx);
- Console.WriteLine("Filosofo {0} Prende forchetta Destra {1}", this.Importanza.ToString(), forchettaDx.Importanza.ToString());
- Monitor.Enter(forchettaSx);
- Console.WriteLine("Filosofo {0} Prende forchetta Sinistra {1}", this.Importanza.ToString(), forchettaSx.Importanza.ToString());
- }
- else
- {
- Monitor.Enter(forchettaSx);
- Console.WriteLine("Filosofo {0} Prende forchetta Sinistra {1}", this.Importanza.ToString(), forchettaSx.Importanza.ToString());
- Monitor.Enter(forchettaDx);
- Console.WriteLine("Filosofo {0} Prende forchetta Destra {1}", this.Importanza.ToString(), forchettaDx.Importanza.ToString());
- }
- Thread.Sleep(randomSleepTime.Next(1, 3000));
- Console.WriteLine("\nFilosofo {0} sta mangiando\n", this.Importanza.ToString());
- if (forchettaDx.Importanza < forchettaSx.Importanza)
- {
- Monitor.Exit(forchettaSx);
- Console.WriteLine("Filosofo {0} Rilascia forchetta Sinistra {1}", this.Importanza.ToString(), forchettaSx.Importanza.ToString());
- Monitor.Exit(forchettaDx);
- Console.WriteLine("Filosofo {0} Rilascia forchetta Destra {1}", this.Importanza.ToString(), forchettaDx.Importanza.ToString());
- }
- else
- {
- Monitor.Exit(forchettaDx);
- Console.WriteLine("Filosofo {0} Rilascia forchetta Destra {1}", this.Importanza.ToString(), forchettaDx.Importanza.ToString());
- Monitor.Exit(forchettaSx);
- Console.WriteLine("Filosofo {0} Rilascia forchetta Sinistra {1}", this.Importanza.ToString(), forchettaSx.Importanza.ToString());
- }
- }
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- HoldForchetta[] holdForc = new HoldForchetta[5];
- holdForc[0] = new HoldForchetta(1);
- holdForc[1] = new HoldForchetta(2);
- holdForc[2] = new HoldForchetta(3);
- holdForc[3] = new HoldForchetta(4);
- holdForc[4] = new HoldForchetta(5);
- Random random = new Random();
- Filosofo[] Filosofo = new Filosofo[5];
- Filosofo[0] = new Filosofo(holdForc[1], holdForc[0], random, 1);
- Filosofo[1] = new Filosofo(holdForc[2], holdForc[1], random, 2);
- Filosofo[2] = new Filosofo(holdForc[3], holdForc[2], random, 3);
- Filosofo[3] = new Filosofo(holdForc[4], holdForc[3], random, 4);
- Filosofo[4] = new Filosofo(holdForc[0], holdForc[4], random, 5);
- Thread FilosofoThread0 = new Thread(new ThreadStart(Filosofo[0].Filosofa));
- Thread FilosofoThread1 = new Thread(new ThreadStart(Filosofo[1].Filosofa));
- Thread FilosofoThread2 = new Thread(new ThreadStart(Filosofo[2].Filosofa));
- Thread FilosofoThread3 = new Thread(new ThreadStart(Filosofo[3].Filosofa));
- Thread FilosofoThread4 = new Thread(new ThreadStart(Filosofo[4].Filosofa));
- FilosofoThread0.Name = "Filosofo 1";
- FilosofoThread1.Name = "Filosofo 2";
- FilosofoThread2.Name = "Filosofo 3";
- FilosofoThread3.Name = "Filosofo 4";
- FilosofoThread4.Name = "Filosofo 5";
- FilosofoThread0.Start();
- FilosofoThread1.Start();
- FilosofoThread2.Start();
- FilosofoThread3.Start();
- FilosofoThread4.Start();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement