Anonim_999

tol

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