Обработка одного массива несколькими потоками - C#
Формулировка задачи:
Здравствуйте, имеется следующий код:
В примере каждый из двоих потоков обрабатывает массив от 1 до 10 (то есть оба потока обрабатывают каждое число). Каким образом сделать чтобы каждый поток обрабатывал один и тот же массив (к примеру первый поток:1, второй поток:2, первый поток:3, второй поток:4 и т.д.). Требуется для сравнения кол-ва времени, которое нужно для обработки одного массива 2-10 потоками.
И еще - каким образом сделать вывод "за сколько времени" каждый поток полностью выполнил обработку, и сколько времени было потрачено обработки всего массива?
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- namespace TestConsoleApp
- {
- class Program
- {
- static void Main(string[] args)
- {
- ThreadStart testThread1Start = new ThreadStart(new Program().testThread1);
- ThreadStart testThread2Start = new ThreadStart(new Program().testThread2);
- Thread[] testThread = new Thread[2];
- testThread[0] = new Thread(testThread1Start);
- testThread[1] = new Thread(testThread2Start);
- foreach (Thread myThread in testThread)
- {
- myThread.Start();
- }
- Console.ReadLine();
- }
- public void testThread1()
- {
- //executing in thread
- int count = 0;
- while (count++ < 10)
- {
- Console.WriteLine("Thread 1 Executed "+count+" times");
- Thread.Sleep(1);
- }
- }
- public void testThread2()
- {
- //executing in thread
- int count = 0;
- while (count++ < 10)
- {
- Console.WriteLine("Thread 2 Executed " + count + " times");
- Thread.Sleep(1);
- }
- }
- }
- }
Решение задачи: «Обработка одного массива несколькими потоками»
textual
Листинг программы
- using System;
- using System.Threading;
- using System.Diagnostics;
- namespace TestConsoleApp
- {
- class Program
- {
- static int[] ar = {0,1,2,3,4,5,6,7,8,9};
- static int _lock = 0;
- static void Main(string[] args)
- {
- ThreadStart testThread1Start = new ThreadStart(new Program().testThread1);
- ThreadStart testThread2Start = new ThreadStart(new Program().testThread2);
- Thread[] testThread = new Thread[2];
- testThread[0] = new Thread(testThread1Start);
- testThread[1] = new Thread(testThread2Start);
- foreach (Thread myThread in testThread)
- {
- myThread.Start();
- }
- Console.ReadLine();
- }
- public void testThread1()
- {
- Stopwatch sw1 = new Stopwatch();
- sw1.Start();
- Thread.Sleep(101);
- while (_lock < 10)
- {
- Console.WriteLine("Thread 1 Executed value {0}", GetNext());
- Thread.Sleep(1);
- }
- sw1.Stop();
- TimeSpan ts = sw1.Elapsed;
- // Format and display the TimeSpan value.
- string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
- ts.Hours, ts.Minutes, ts.Seconds,
- ts.Milliseconds / 10);
- Console.WriteLine("RunTime Thread 1 {0}", elapsedTime);
- }
- public void testThread2()
- {
- Stopwatch sw2 = new Stopwatch();
- sw2.Start();
- Thread.Sleep(100);
- while (_lock < 10)
- {
- Console.WriteLine("Thread 2 Executed value {0}", GetNext());
- Thread.Sleep(1);
- }
- sw2.Stop();
- TimeSpan ts = sw2.Elapsed;
- // Format and display the TimeSpan value.
- string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
- ts.Hours, ts.Minutes, ts.Seconds,
- ts.Milliseconds / 10);
- Console.WriteLine("RunTime Thread 2 {0}", elapsedTime);
- }
- static int GetNext()
- {
- int a = ar[_lock];
- _lock += 1;
- return a;
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д