欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#控制台下测试多线程的方法

程序员文章站 2023-12-12 14:47:34
本文实例讲述了c#控制台下多线程实现方法。分享给大家供大家参考。具体如下: class program { static void main(string...

本文实例讲述了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#程序设计有所帮助。

上一篇:

下一篇: