Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- namespace _5Filosofi
- {
- class HoldForchettaUnsyncronized
- {
- private int forchetta = -1;
- private int importanza;
- public int Forchetta
- {
- get
- {
- Console.WriteLine(Thread.CurrentThread.Name + " legge " + forchetta);
- return forchetta;
- }
- set
- {
- Console.WriteLine(Thread.CurrentThread.Name + " scrive " + value);
- forchetta = value;
- }
- }
- public HoldForchettaUnsyncronized(int num)
- {
- importanza = num;
- }
- }
- class Filosofo
- {
- private HoldForchettaUnsyncronized sharedLocation;
- private Random randomSleepTime;
- public Filosofo(HoldForchettaUnsyncronized shared, Random random)
- {
- sharedLocation = shared;
- randomSleepTime = random;
- }
- public void Riposiziona()
- {
- for (int count = 1; count <= 2; count++)
- {
- Thread.Sleep(randomSleepTime.Next(1, 3000));
- sharedLocation.Forchetta = count;
- }
- Console.WriteLine(Thread.CurrentThread.Name + " Utilizzata.");
- }
- public void Utilizza()
- {
- int sum = 0;
- for (int count = 1; count <= 4; count++)
- {
- Thread.Sleep(randomSleepTime.Next(1, 3000));
- sum += sharedLocation.Forchetta;
- }
- Console.WriteLine(Thread.CurrentThread.Name + " read values totalng: " + sum);
- Console.WriteLine("Terminating " + Thread.CurrentThread.Name);
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- HoldForchettaUnsyncronized[] holdForc = new HoldForchettaUnsyncronized[5];
- holdForc[0] = new HoldForchettaUnsyncronized(1);
- holdForc[1] = new HoldForchettaUnsyncronized(2);
- holdForc[2] = new HoldForchettaUnsyncronized(3);
- holdForc[3] = new HoldForchettaUnsyncronized(4);
- holdForc[4] = new HoldForchettaUnsyncronized(5);
- Random random = new Random();
- Filosofo[] Filosofo = new Filosofo[5];
- Filosofo Filosofo = new Filosofo(holdForc, random);
- Thread Utilizza = new Thread(new ThreadStart(Filosofo.Utilizza));
- Thread Riposiziona = new Thread(new ThreadStart(Filosofo.Riposiziona));
- Riposiziona.Name = "Riposiziona";
- Utilizza.Name = "Utilizza";
- Utilizza.Start();
- Riposiziona.Start();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement