Jason_liu

导航

C#动态多线程创建(勇哥讲解)

using System;
using System.Collections.Generic;
using System.Text;
using System;
using System.Threading;

namespace Test
{
class Program
{
  class ThreadProc
  {
    int _i = 0;

    public ThreadProc(int i)
    {
      this._i = i;
    }

    public void thread_proc()
    {
      while (true)
      {
        Console.WriteLine("This is thread {0}. Current Time is {1}.",
        _i, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"));

        Thread.Sleep(10000);
      }
    }
  }

    static void Main(string[] args)
    {
      int MAX_THREADS = 70;
      ThreadProc[] tproc = new ThreadProc[MAX_THREADS];
      Thread[] oThread = new Thread[MAX_THREADS];

      for (int i = 0; i < MAX_THREADS; i++)
      {
        tproc[i] = new ThreadProc(i);
        oThread[i] = new Thread(new ThreadStart(tproc[i].thread_proc));
        oThread[i].Start();
      }

      Thread.Sleep(300000);

      for (int i = 0; i < MAX_THREADS; i++)
      {
        oThread[i].Abort();
      }
    }
  }
}

posted on 2012-09-08 01:32  Jason_liu  阅读(1344)  评论(0编辑  收藏  举报