下面是開啟了兩個線程,就以這兩個線程為例,進行測試吧
static void Main(string[] args)
{
ThreadStart sptintnumb = new ThreadStart(PrintNumb);
Thread tnumb = new Thread(sptintnumb);
ThreadStart sprintstr = new ThreadStart(PrintStr);
Thread trintstr = new Thread(sprintstr);
Stopwatch watch = new Stopwatch();
watch.Start();
trintstr.Start();
tnumb.Start();
//起一個循環,不斷判斷線程的執行狀態
while (true)
{
if(trintstr.ThreadState==System.Threading.ThreadState.Stopped && tnumb.ThreadState==System.Threading.ThreadState.Stopped)
{
watch.Stop();
Console.WriteLine(watch.Elapsed.TotalMilliseconds);
break;
}
}
Console.ReadKey();
}
private static void PrintNumb()
{
for (int i = 0; i < 1000; i++)
{
Console.WriteLine(i);
}
}
private static void PrintStr()
{
for (int i = 0; i < 1000; i++)
{
Console.WriteLine("你輸入的是:"+i.ToString());
}
}