C#控制台下多线程 C#控制台下测试多线程的方法
feige 人气:0想了解C#控制台下测试多线程的方法的相关内容吗,feige在本文为您仔细讲解C#控制台下多线程的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:C#,控制台,多线程,下面大家一起来学习吧。
本文实例讲述了C#控制台下多线程实现方法。分享给大家供大家参考。具体如下:
class Program { static void Main(string[] args) { ThreadStart num = new ThreadStart(PrintNum); Thread ConstrolNum = new Thread(num); ThreadStart str = new ThreadStart(PrintStr); Thread ConstrolStr = new Thread(str); Stopwatch watch = new Stopwatch(); watch.Start(); ConstrolNum.Start(); ConstrolStr.Start(); while (true) { if (ConstrolNum.ThreadState == System.Threading.ThreadState.Stopped && ConstrolStr.ThreadState == System.Threading.ThreadState.Stopped) { watch.Stop(); Console.WriteLine(watch.Elapsed.TotalMilliseconds); break; } } Console.ReadKey(); } private static void PrintNum() { for (int i = 1; i < 1000; i++) { Console.WriteLine(i); } } private static void PrintStr() { for (int i = 1; i < 1000; i++) { Console.WriteLine("当前数为:{0}", i); } } }
希望本文所述对大家的C#程序设计有所帮助。
加载全部内容