Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- static BigInteger totalResult = new BigInteger(1);
- static object ForLock = 1;
- static void Main(string[] args)
- {
- int count;
- Console.WriteLine("Введите количество потоков >=1");
- count = Convert.ToInt32(Console.ReadLine());
- DateTime startTime = DateTime.Now;
- int a = 100000;
- List<Thread> threads = new List<Thread>();
- for (int i = count; i > 0; i--)
- {
- Thread th = new Thread(Fact);
- th.Name = (i).ToString();
- threads.Add(th);
- th.Start(new int[] { a, a - a / i });
- a -= a / i;
- }
- foreach (Thread th in threads)
- {
- th.Join();
- }
- Console.WriteLine("Все потоки завершились!");
- Console.WriteLine("Затрачено" + (startTime - DateTime.Now).ToString());
- Console.WriteLine("Результат - " + totalResult.ToString());
- }
- static void Fact(object param)
- {
- int start = (param as int[])[0];
- int end = (param as int[])[1];
- Console.WriteLine("[{0}] {1} - {2}", Thread.CurrentThread.Name, start, end);
- BigInteger res = new BigInteger(1);
- for (int b = start; b > end; b--)
- {
- res = res * b;
- }
- lock (ForLock)
- {
- totalResult *= res;
- }
- Console.WriteLine("Поток №{0} заврешился", Thread.CurrentThread.Name);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement