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); } }
时间: 2024-10-07 16:42:22