ThreadStart   和ParameterizedThreadStart

 

 不带参数的多线程,这里使用了三个线程

 class Program
    {
        static void Main()
        {
            Thread newThread;
            ThreadStart threadMethod = new ThreadStart(DoWork);
            ThreadStart threadEnglish = new ThreadStart(DoEnglish);
        
            for (int counter = 1; counter < 4; counter++)
            {
                Console.WriteLine("加油 {0}", counter);

                newThread = new Thread(threadMethod);
                newThread.Name = counter.ToString();
                newThread.Start();
                newThread = new Thread(threadEnglish);
                newThread.Name = counter.ToString();
                newThread.Start();
            }
        }
        static void DoWork()
        {
            for (int mm= 1; mm < 11; mm++)
            {
                Console.WriteLine("Thread {0}: 循环{1}", Thread.CurrentThread.Name,mm);
                Thread.Sleep(1);
            }
        }
        static void DoEnglish()
        {
            string EnglishString = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
            string[] ES=EnglishString .Split(new char[]{','});
            for (int i=0;i<ES.Length ;i++)
            {
                Console.WriteLine(ES[i]);
                Thread.Sleep(1);
            }
        }
    }

 

 

 

 这里使用带参数的线程,创建了三个线程

class Program
    {
        static void Main()
        {
            Thread newThread;
            ParameterizedThreadStart threadMethod = new ParameterizedThreadStart(DoWork);
            for (int counter = 1; counter < 4; counter++)
            {
                Console.WriteLine( counter);
                newThread = new Thread(threadMethod);
                newThread.Name = counter.ToString();
                string EnglishString = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
              
                newThread.Start(EnglishString);
            }
        }
        static void DoWork(object  iteartions)
        {
            string[] ES = iteartions.ToString ().Split(new char[] { ',' });
            for (int counter = 1; counter < ES.Length; counter++)
            {
                Console.WriteLine("Thread {0}: iteration {1}", Thread.CurrentThread.Name, counter);
                Thread.Sleep(1);
            }
        }
    }

posted on 2010-11-30 13:04  挑战自我  阅读(144)  评论(0编辑  收藏  举报