多线程简单任务_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > 多线程简单任务

多线程简单任务

 2013/9/3 15:15:19  行者之刃  博客园  我要评论(0)
  • 摘要:classProgram{staticAutoResetEventare=newAutoResetEvent(false);staticintticks=100;staticvoidMain(string[]args){Threadt1=newThread(newThreadStart(Saling));Threadt2=newThread(newThreadStart(Saling));Threadt3=newThread(newThreadStart(Saling))
  • 标签:多线程 线程
class="brush:csharp;gutter:false;"> class Program
    {
      static  AutoResetEvent are = new AutoResetEvent(false);
      static int ticks = 100;
        static void Main(string[] args)
        {
            Thread t1 = new Thread(new ThreadStart(Saling));
            Thread t2 = new Thread(new ThreadStart(Saling));
            Thread t3 = new Thread(new ThreadStart(Saling));
            Thread t4 = new Thread(new ThreadStart(Saling));
            t1.Name = "1";
            t2.Name = "2";
            t3.Name = "3";
            t4.Name = "4";
            t1.Start();
            t2.Start();
            t3.Start();
            t4.Start();
            Console.ReadKey();
        }
        public static void Saling()
        {
            Thread th = Thread.CurrentThread; //得到当前线程
            while(ticks>-1)
            {
                Console.WriteLine("ThreadName:"+th.Name+" 票数:"+ticks--);
                are.Set();//终止当前线程,让下一个线程来执行
                Thread.Sleep(50);
            }
        }
    }

 

发表评论
用户名: 匿名