Part 97   Performance of a multithreaded program

Part 97   Performance of a multithreaded program

class Program
    {
        static void Main(string[] args)
        {
            Stopwatch s = new Stopwatch();
            s.Start();
            EvenNumbersSum();
            OddNumbersSum();
            s.Stop();
            Console.WriteLine("before using multiple threads"+s.ElapsedMilliseconds);
            s = new Stopwatch();
            s.Start();
            Thread t1 = new Thread(EvenNumbersSum);
            Thread t2 = new Thread(OddNumbersSum);
            t1.Start();
            t2.Start();
            t1.Join();
            t2.Join();
            s.Stop();
            Console.WriteLine("after using multiple threads"+s.ElapsedMilliseconds);

        }

        public static void EvenNumbersSum()
        {
            double sum=0;
            for(int i=0;i<=50000000;i++)
            {
                if(i%2==0)
                {
                    sum += i;
                }
            }
            Console.WriteLine("sum= "+sum);
        }

        public static void OddNumbersSum()
        {
            double sum = 0;
            for (int i = 0; i <= 50000000; i++)
            {
                if (i % 2 == 1)
                {
                    sum += i;
                }
            }
            Console.WriteLine("sum= " + sum);
        }
    }
View Code

相关文章:

  • 2022-12-23
  • 2021-08-13
  • 2021-09-13
  • 2021-09-13
  • 2021-10-03
  • 2022-12-23
  • 2021-06-12
  • 2022-02-28
猜你喜欢
  • 2021-12-03
  • 2021-11-12
  • 2021-09-26
  • 2021-09-14
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案