Anonim_999

Alex

Nov 23rd, 2022
752
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.79 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3.  
  4. namespace ConsoleApp19
  5. {
  6.     class Program
  7.     {
  8.         public static int[] A;
  9.         public static int countNumbers;
  10.         public static int countThread;
  11.         public static object Locker = new object();
  12.         public static int subArrayLen;
  13.         public static int result = 0;
  14.  
  15.         static void Main(string[] args)
  16.         {
  17.             Console.Write("Количество цифр: ");
  18.  
  19.             if (int.TryParse(Console.ReadLine(), out countNumbers) == false || countNumbers < 1)
  20.             {
  21.                 countNumbers = 10;
  22.                 Console.WriteLine($"Нельзя такое писать,количество чисел - {countNumbers}");
  23.             }
  24.             A = new int[countNumbers];
  25.             Console.WriteLine("Задать числам значение:");
  26.             UserFillA(ref A);
  27.             Console.WriteLine("A: ");
  28.  
  29.             for (int i = 0; i < countNumbers; i++)
  30.                 Console.Write($"{A[i]} ");
  31.             Console.Write("\nКоличество потоков: ");
  32.  
  33.             if (int.TryParse(Console.ReadLine(), out countThread) == false || countThread < 1)
  34.             {
  35.                 countThread = 1;
  36.                 Console.WriteLine($"Нельзя такое писать, количество потоков - {countThread}");
  37.             }
  38.  
  39.             if (countThread > countNumbers)
  40.             {
  41.                 countThread = countNumbers;
  42.                 Console.WriteLine("Слишком много потоков, количество потоков - {count}");
  43.             }
  44.             subArrayLen = countNumbers / countThread + 1;
  45.             Thread[] threads = new Thread[countThread];
  46.             StartAllThreads(ref threads);
  47.             WaitAllThreads(ref threads);
  48.             Console.WriteLine($"Количество элементов кратных пяти: {result}");
  49.             Console.ReadLine();
  50.         }
  51.  
  52.         private static void WaitAllThreads(ref Thread[] threads)
  53.         {
  54.             foreach (Thread item in threads)
  55.             {
  56.                 Console.WriteLine($"{item.Name} - enabled");
  57.                 item.Join();
  58.                 Console.WriteLine($"{item.Name} - switched off");
  59.             }
  60.         }
  61.  
  62.         private static void StartAllThreads(ref Thread[] threads)
  63.         {
  64.             for (int i = 0; i < countThread; i++)
  65.             {
  66.                 PartArray partArray = new PartArray
  67.                 {
  68.                     StartIndex = i * subArrayLen,
  69.                     EndIndex = (i + 1) * subArrayLen
  70.                 };
  71.  
  72.                 if (partArray.EndIndex > countNumbers)
  73.                     partArray.EndIndex = countNumbers;
  74.                 threads[i] = new Thread(() => SumWith3(partArray));
  75.                 threads[i].Name = $"Поток№{i + 1}";
  76.                 threads[i].Start();
  77.             }
  78.         }
  79.  
  80.         private static void UserFillA(ref int[] array)
  81.         {
  82.             for (int i = 0; i < array.Length; i++)
  83.             {
  84.                 Console.Write($"{i + 1}:");
  85.  
  86.                 if (int.TryParse(Console.ReadLine(), out array[i]) == false || array[i] < 1)
  87.                 {
  88.                     Console.WriteLine("Неправильный ввод");
  89.                     i--;
  90.                 }
  91.             }
  92.         }
  93.  
  94.         private static void SumWith3(PartArray partArray)
  95.         {
  96.             int start = partArray.StartIndex;
  97.             int end = partArray.EndIndex;
  98.  
  99.             for (int i = start; i < end; i++)
  100.             {
  101.                 if (A[i] % 5 == 0)
  102.                     lock (Locker)
  103.                         result++;
  104.             }
  105.         }
  106.     }
  107.  
  108.     class PartArray
  109.     {
  110.         public int StartIndex;
  111.         public int EndIndex;
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment