Anonim_999

safonovOplatit'

Nov 19th, 2022
1,171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.49 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 countElements = 1;
  10.         public static int countThreads = 1;
  11.         public static int subArrayLength;
  12.         public static int sum = 0;
  13.         public static object Locker = new object();
  14.  
  15.         static void Main(string[] args)
  16.         {
  17.             string userInput;
  18.             Console.WriteLine("Размерность A: ");
  19.             userInput = Console.ReadLine();
  20.  
  21.             if (int.TryParse(userInput,out countElements) == false || countElements <= 0)
  22.             {
  23.                 countElements = 10;
  24.                 Console.WriteLine($"Не вводите буквы или значения <= 0. Количество элементов - {countElements}");
  25.             }
  26.             A = new int[countElements];
  27.             FillArray(ref A);
  28.             ViewArray(A);
  29.             Console.WriteLine("\nКоличество потоков: ");
  30.             userInput = Console.ReadLine();
  31.  
  32.             if (int.TryParse(userInput,out countThreads) == false || countThreads <= 0)
  33.             {
  34.                 countThreads = 1;
  35.                 Console.WriteLine($"Не вводите буквы или значения <= 0. Количество потоков - {countThreads}");
  36.             }
  37.  
  38.             if (countThreads > countElements)
  39.                 countThreads = countElements;
  40.             subArrayLength = countElements / countThreads + 1;
  41.  
  42.             Thread[] flows = new Thread[countThreads];
  43.  
  44.             for (int i = 0; i < countThreads; i++)
  45.             {
  46.                 PartArray partArray = new PartArray
  47.                 {
  48.                     StartIndex = i * subArrayLength,
  49.                     EndIndex = (i + 1) * subArrayLength
  50.                 };
  51.  
  52.                 if (partArray.EndIndex > countElements)
  53.                     partArray.EndIndex = countElements;
  54.                 flows[i] = new Thread(() => Sum(partArray));
  55.                 flows[i].Name = $"Поток №{i + 1}";
  56.                 flows[i].Start();
  57.             }
  58.  
  59.             WaitFlows(flows);
  60.             Console.WriteLine($"Результат: {sum}");
  61.         }
  62.  
  63.         public static void WaitFlows(Thread[] threads)
  64.         {
  65.             for (int i = 0; i < threads.Length; i++)
  66.             {
  67.                 Console.WriteLine($"{threads[i].Name} - on");
  68.                 threads[i].Join();
  69.                 Console.WriteLine($"{threads[i].Name} - off");
  70.             }
  71.         }
  72.  
  73.         public static void FillArray(ref int[] array)
  74.         {
  75.             Random random = new Random();
  76.  
  77.             for(int i = 0; i < array.Length; i++)
  78.                 array[i] = random.Next(countElements);
  79.         }
  80.  
  81.         public static void ViewArray(int[] array)
  82.         {
  83.             foreach (int item in array)
  84.                 Console.Write($"{item} ");
  85.         }
  86.  
  87.         public static void Sum(PartArray partArray)
  88.         {
  89.             int startIndex = partArray.StartIndex;
  90.             int endIndex = partArray.EndIndex;
  91.  
  92.             for (int i = startIndex; i < endIndex; i++)
  93.             {
  94.                 if (A[i] % 2 == 0)
  95.                 {
  96.                     lock (Locker)
  97.                     {
  98.                         sum += A[i];
  99.                     }
  100.                 }
  101.             }
  102.         }
  103.     }
  104.  
  105.     public class PartArray
  106.     {
  107.         public int StartIndex;
  108.         public int EndIndex;
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment