Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Threading;
- namespace ConsoleApp1
- {
- class Program
- {
- static SemaphoreSlim S = new SemaphoreSlim(1);
- static int postiLiberi = 200;
- static void Main(string[] args)
- {
- Parallel.Invoke(() => Cassa(1), () => Cassa(2));
- }
- static void Cassa(int numCassa)
- {
- int nb;
- Random r = new Random(numCassa);
- for (int i=0; i < 20; i++)
- {
- Thread.Sleep(r.Next(10, 15 + 1));
- nb = r.Next(1, 10 + 1);
- Console.WriteLine("cassa num: " + numCassa.ToString());
- Occupa(nb);
- }
- Console.WriteLine("cassa " + numCassa.ToString() + " chiusa");
- }
- static void Occupa(int quanti)
- {
- S.Wait();
- if (postiLiberi >= quanti)
- {
- postiLiberi -= quanti;
- Console.WriteLine("assegnati " + quanti.ToString() + " posti");
- }
- else
- {
- Console.WriteLine("posti non disponibili");
- }
- S.Release();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement