Anonim_999

CHZH

Nov 22nd, 2022
856
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.32 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3.  
  4. namespace ConsoleApp19
  5. {
  6.     class Program
  7.     {
  8.         public static int CountElements;
  9.         public static int ThreadCount;
  10.         public static int[] ArrayNumbers;
  11.         public static bool Switcher = true;
  12.         public static int Result = 0;
  13.         public static object Locker = new object();
  14.         public static int TempEndIndex = 0;
  15.  
  16.         static void Main(string[] args)
  17.         {
  18.             int subArrayLenght = 0;
  19.             string userInput;
  20.             Random random = new Random();
  21.             Console.Write("Array lenght: ");
  22.  
  23.             if (!int.TryParse(Console.ReadLine(), out CountElements) || CountElements < 1)
  24.             {
  25.                 Console.WriteLine("Error. Array lenght - 10");
  26.                 CountElements = 10;
  27.             }
  28.             Console.Write("Threads count: ");
  29.  
  30.             if (!int.TryParse(Console.ReadLine(), out ThreadCount) || CountElements < 1)
  31.             {
  32.                 Console.WriteLine("Error. Threads count - 1");
  33.                 ThreadCount = 1;
  34.             }
  35.             ArrayNumbers = new int[CountElements];
  36.  
  37.             for (int i = 0; i < CountElements - 1; i++)
  38.             {
  39.                 Console.Write($"{i + 1}:");
  40.                 userInput = Console.ReadLine();
  41.  
  42.                 if (!int.TryParse(userInput, out ArrayNumbers[i]) || ArrayNumbers[i] < 1)
  43.                 {
  44.                     Console.WriteLine($"Error. {i} value must be numeric");
  45.                     i--;
  46.                 }
  47.             }
  48.             Console.WriteLine("Array elements: ");
  49.  
  50.             foreach (int item in ArrayNumbers)
  51.             {
  52.                 Console.Write($"{item} ");
  53.             }
  54.  
  55.             if (ThreadCount <= 0)
  56.                 ThreadCount = 1;
  57.  
  58.             if (ThreadCount > CountElements)
  59.                 ThreadCount = CountElements;
  60.             subArrayLenght = CountElements / ThreadCount + 1;
  61.             Thread[] threads = new Thread[ThreadCount];
  62.             Console.WriteLine();
  63.  
  64.             for (int i = 0; i < ThreadCount; i++) // Создание потоков
  65.             {
  66.                 PartArray partArray = new PartArray
  67.                 {
  68.                     StartIndex = i * subArrayLenght,
  69.                     EndIndex = (i + 1) * subArrayLenght
  70.                 };
  71.  
  72.                 if (partArray.EndIndex > CountElements)
  73.                     partArray.EndIndex = CountElements;
  74.                 threads[i] = new Thread(() => EvaluateExpression(partArray)); //Передача метода потоку
  75.                 threads[i].Name = $"Поток №{i + 1}";
  76.             }
  77.             Console.WriteLine();
  78.  
  79.             for (int i = 0; i < ThreadCount; i++)
  80.             {
  81.                 Console.WriteLine($"{threads[i].Name} - start");
  82.                 threads[i].Start(); //Запуск потока
  83.                 threads[i].Join(); //Ожидание завершения потока
  84.                 Console.WriteLine($"{threads[i].Name} - finish");
  85.             }
  86.             Console.WriteLine($"Result: {Result}");
  87.             Console.ReadLine();
  88.         }
  89.  
  90.         static void EvaluateExpression(PartArray partArray) //Метод расчета
  91.         {
  92.             if (partArray == null) //Проверка на пустоту класса
  93.                 return;
  94.             int start = partArray.StartIndex;
  95.             int end = partArray.EndIndex;
  96.  
  97.             for (int i = start; i < end; i++)
  98.             {
  99.                 if (Switcher)
  100.                 {
  101.                     lock (Locker) //блокирует объекты, чтобы не было конфликтов с потоками
  102.                     {
  103.                         Result += ArrayNumbers[i];
  104.                         Switcher = false;
  105.                     }
  106.                 }
  107.                 else
  108.                 {
  109.                     lock (Locker)//блокирует объекты, чтобы не было конфликтов с потоками
  110.                     {
  111.                         Result -= ArrayNumbers[i];
  112.                         Switcher = true;
  113.                     }
  114.                 }
  115.             }
  116.         }
  117.     }
  118. }
  119.  
  120. public class PartArray //Класс для хранения интервала
  121. {
  122.     public int StartIndex;
  123.     public int EndIndex;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment