你的位置:首页 > ASP.net教程

[ASP.net教程]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