Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.70 KB | None | 0 0
  1. class Program
  2.     {
  3.         static BigInteger totalResult = new BigInteger(1);
  4.         static object ForLock = 1;
  5.  
  6.         static void Main(string[] args)
  7.         {
  8.             int count;
  9.             Console.WriteLine("Введите количество потоков >=1");
  10.             count = Convert.ToInt32(Console.ReadLine());
  11.             DateTime startTime = DateTime.Now;
  12.             int a = 100000;
  13.             List<Thread> threads = new List<Thread>();
  14.             for (int i = count; i > 0; i--)
  15.             {
  16.                 Thread th = new Thread(Fact);
  17.                 th.Name = (i).ToString();
  18.                 threads.Add(th);
  19.                 th.Start(new int[] { a, a - a / i });
  20.                 a -= a / i;
  21.             }
  22.             foreach (Thread th in threads)
  23.             {
  24.                 th.Join();
  25.             }
  26.             Console.WriteLine("Все потоки завершились!");
  27.             Console.WriteLine("Затрачено" + (startTime - DateTime.Now).ToString());
  28.             Console.WriteLine("Результат - " + totalResult.ToString());
  29.         }
  30.         static void Fact(object param)
  31.         {
  32.             int start = (param as int[])[0];
  33.             int end = (param as int[])[1];
  34.             Console.WriteLine("[{0}] {1} - {2}", Thread.CurrentThread.Name, start, end);
  35.  
  36.             BigInteger res = new BigInteger(1);
  37.  
  38.             for (int b = start; b > end; b--)
  39.             {
  40.                 res = res * b;
  41.             }
  42.  
  43.             lock (ForLock)
  44.             {
  45.                 totalResult *= res;
  46.             }
  47.  
  48.             Console.WriteLine("Поток №{0} заврешился", Thread.CurrentThread.Name);
  49.         }
  50.  
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement