public void DoJob()
        {
            var dataArray = Enumerable.Range(1, 20).ToArray();
            var sum       = 0;
            var watch     = new Stopwatch();

            watch.Start();

            foreach (var data in dataArray)
            {
                sum += TestParallel.TestOperation(data);
            }

            watch.Stop();

            Console.WriteLine($"Sync operation: Result: {sum}, Time: {watch.ElapsedMilliseconds}.");
        }
示例#2
0
        public void DoJob()
        {
            var dataArray  = Enumerable.Range(1, 20).ToArray();
            var arrayCount = dataArray.Length;
            var sum        = 0;
            var i          = 0;
            var watch      = new Stopwatch();

            watch.Start();

            foreach (var data in dataArray)
            {
                var thread = new Thread(() =>
                {
                    var result = TestParallel.TestOperation(data);

                    lock (Lock)
                    {
                        sum += result;
                        i   += 1;
                    }
                });

                thread.Start();
            }

            while (true)
            {
                if (i >= arrayCount)
                {
                    break;
                }
            }

            watch.Stop();

            Console.WriteLine($"Sync operation: Result: {sum}, Time: {watch.ElapsedMilliseconds}.");
        }