ColonelNV

многопоток

Dec 8th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.98 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Threading;
  4.  
  5. namespace ConsoleApp1
  6. {
  7.     class ConsoleApp1
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Console.WriteLine("Введите кол-во строк: ");
  12.             int n = Convert.ToInt32(Console.ReadLine());
  13.             Console.WriteLine("Введите кол-во столбцов: ");
  14.             int m = Convert.ToInt32(Console.ReadLine());
  15.             double N = 0;
  16.             double t1 = 0, t2 = 0, t3 = 0, t4 = 0;
  17.             double s=0;
  18.             var timer = Stopwatch.StartNew();
  19.             void FirstThread()
  20.             {
  21.                 for (int i = 1; i < (((n ) / 4) + 1); i++)
  22.                 {
  23.                     s = 0;
  24.                     for (int j = 1; j < m + 1; j++)
  25.                     {
  26.                         s = Math.Sqrt((i-1)+(j-1));
  27.                         t1 += s;
  28.                     }
  29.                 }
  30.             }
  31.             void SecondThread()
  32.             {
  33.                 for (int i = (((n) / 4) + 1); i < ((n) / 2) + 1; i++)
  34.                 {
  35.                     s = 0;
  36.                     for (int j = 1; j < m + 1; j++)
  37.                     {
  38.                         s = Math.Sqrt((i-1)+(j-1));
  39.                         t2 += s;
  40.                     }
  41.                 }
  42.             }
  43.             void ThirdThread()
  44.             {
  45.                 for (int i = ((n) / 2) + 1; i < ((3 * (n)) / 4) + 1; i++)
  46.                 {
  47.                     s = 0;
  48.                     for (int j = 1; j < m + 1; j++)
  49.                     {
  50.                         s = Math.Sqrt((i-1)+(j-1));
  51.                         t3 += s;
  52.                     }
  53.                 }
  54.             }
  55.             void FourthThread()
  56.             {
  57.                 for (int i = (3 * (n)) / 4 + 1; i < (n + 1); i++)
  58.                 {
  59.                     s = 0;
  60.                     for (int j = 1; j < m + 1; j++)
  61.                     {
  62.                         s = Math.Sqrt((i-1)+(j-1));
  63.                         t4 += s;
  64.                     }
  65.                 }
  66.             }
  67.             Thread thr1 = new Thread(() => FirstThread());
  68.             Thread thr2 = new Thread(() => SecondThread());
  69.             Thread thr3 = new Thread(() => ThirdThread());
  70.             Thread thr4 = new Thread(() => FourthThread());
  71.             thr1.Start();
  72.             Thread.Sleep(100);
  73.             thr2.Start();
  74.             Thread.Sleep(100);
  75.             thr3.Start();
  76.             Thread.Sleep(100);
  77.             thr4.Start();
  78.             Thread.Sleep(100);
  79.             N = t1 * t2 * t3 * t4;
  80.             Console.WriteLine("Результат - " + (N - N % 0.001));
  81.             timer.Stop();
  82.             Console.WriteLine("Выполнение метода заняло мс: {0}", timer.ElapsedMilliseconds - 400);
  83.             Console.WriteLine("Выполнение метода заняло тактов: {0}", timer.ElapsedTicks-400000);
  84.             Console.ReadKey();
  85.         }
  86.     }
  87. }
Add Comment
Please, Sign In to add comment