Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.29 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Threading;
  7.  
  8. namespace ConsoleApp1
  9. {
  10.     class Program
  11.     {
  12.  
  13.         static SemaphoreSlim S = new SemaphoreSlim(1);
  14.         static int postiLiberi = 200;
  15.  
  16.         static void Main(string[] args)
  17.         {
  18.             Parallel.Invoke(() => Cassa(1), () => Cassa(2));
  19.         }
  20.  
  21.         static void Cassa(int numCassa)
  22.         {
  23.             int nb;
  24.             Random r = new Random(numCassa);
  25.  
  26.             for (int i=0; i < 20; i++)
  27.             {
  28.                 Thread.Sleep(r.Next(10, 15 + 1));
  29.                 nb = r.Next(1, 10 + 1);
  30.                 Console.WriteLine("cassa num: " + numCassa.ToString());
  31.                 Occupa(nb);
  32.             }
  33.             Console.WriteLine("cassa " + numCassa.ToString() + " chiusa");
  34.         }
  35.  
  36.         static void Occupa(int quanti)
  37.         {
  38.             S.Wait();
  39.             if (postiLiberi >= quanti)
  40.             {
  41.                 postiLiberi -= quanti;
  42.                 Console.WriteLine("assegnati " + quanti.ToString() + " posti");
  43.             }
  44.             else
  45.             {
  46.                 Console.WriteLine("posti non disponibili");
  47.             }
  48.             S.Release();
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement