Anonim_999

NikitaClear

Nov 22nd, 2022 (edited)
854
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.14 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 CountElements;
  11.         public static int CountThreads;
  12.         public static int Result = 0;
  13.         public static object Locker = new object();
  14.  
  15.         static void Main(string[] args)
  16.         {
  17.             string input;
  18.             int subArrayLen = 0;
  19.             Console.Write("Arrays count elements: ");
  20.             input = Console.ReadLine();
  21.  
  22.             if (!int.TryParse(input,out CountElements) || CountElements <= 0)
  23.             {
  24.                 CountElements = 10;
  25.                 Console.ForegroundColor = ConsoleColor.Red;
  26.                 Console.WriteLine($"Input error. count elements - {CountElements}");
  27.                 Console.ResetColor();
  28.             }
  29.             A = new int[CountElements];
  30.             C = new int[CountElements];
  31.             Console.Write("Threads count: ");
  32.             input = Console.ReadLine();
  33.  
  34.             if (!int.TryParse(input,out CountThreads) || CountThreads <= 0)
  35.             {
  36.                 CountThreads = 1;
  37.                 Console.ForegroundColor = ConsoleColor.Red;
  38.                 Console.WriteLine($"Input error. count threads - {CountThreads}");
  39.                 Console.ResetColor();
  40.             }
  41.  
  42.             if (CountThreads > CountElements)
  43.             {
  44.                 CountThreads = CountElements;
  45.                 Console.ForegroundColor = ConsoleColor.Red;
  46.                 Console.WriteLine($"Threads count bigger then count elements. Threads count - {CountThreads}");
  47.                 Console.ResetColor();
  48.             }
  49.             subArrayLen = CountElements / CountThreads + 1;
  50.             Console.WriteLine("Fill array A:");
  51.             FillArray(ref A);
  52.             Console.WriteLine("Fill array C:");
  53.             FillArray(ref C);
  54.             Console.WriteLine("A:");
  55.             ShowArray(A);
  56.             Console.WriteLine("\nC:");
  57.             ShowArray(C);
  58.             Thread[] threads = new Thread[CountThreads];
  59.  
  60.             for (int i = 0; i < CountThreads; i++)
  61.             {
  62.                 PartArray partArray = new PartArray
  63.                 {
  64.                     StartIndex = i * subArrayLen,
  65.                     EndIndex = (i + 1) * subArrayLen
  66.                 };
  67.  
  68.                 if (partArray.EndIndex > CountElements)
  69.                     partArray.EndIndex = CountElements;
  70.                 threads[i] = new Thread(() => GetProductNumbers(partArray));
  71.                 threads[i].Name = $"Поток№{i + 1}";
  72.                 threads[i].Start();
  73.             }
  74.             Console.WriteLine();
  75.  
  76.             foreach (Thread thread in threads)
  77.             {
  78.                 Console.WriteLine($"{thread.Name} - on");
  79.                 thread.Join();
  80.                 Console.WriteLine($"{thread.Name} - off");
  81.             }
  82.             Console.WriteLine($"Result of product: {Result}");
  83.             Console.ReadLine();
  84.         }
  85.  
  86.         private static void FillArray(ref int[] A)
  87.         {
  88.             Random random = new Random();
  89.  
  90.             for (int i = 0; i < A.Length; i++)
  91.             {
  92.                 Console.Write($"{i + 1} element:");
  93.  
  94.                 if (!int.TryParse(Console.ReadLine(), out A[i]))
  95.                 {
  96.                     A[i] = random.Next(1, 10);
  97.                     Console.ForegroundColor = ConsoleColor.Red;
  98.                     Console.WriteLine($"Input error. element - {A[i]}");
  99.                     Console.ResetColor();
  100.                 }
  101.             }
  102.         }
  103.  
  104.         private static void ShowArray(int[] array)
  105.         {
  106.             foreach (int item in array)
  107.                 Console.Write($"{item} ");
  108.         }
  109.  
  110.         private static void GetProductNumbers(PartArray partArray)
  111.         {
  112.             int start = partArray.StartIndex;
  113.             int end = partArray.EndIndex;
  114.  
  115.             for (int i = start; i < end; i++)
  116.                 lock (Locker)
  117.                     Result += A[i] * C[i];
  118.         }
  119.     }
  120.  
  121.     class PartArray
  122.     {
  123.         public int StartIndex;
  124.         public int EndIndex;
  125.     }
  126. }
Advertisement
Comments
  • xxea17
    3 years
    Comment was deleted
Add Comment
Please, Sign In to add comment