C#线程的启动

在.net中为我们提供了两种启动线程的方式,一种是不带参数的启动方式,另一种是带参数的启动的方式。

1:不带参数的启动方式,可以使用ThreadStart来实例化Thread,ThreadStart是在.Net Framework 中已经定义好的委托,ThreadStart定义为:

public delegate void ThreadStart();

使用方法如下面的代码:

 static void Main(string[] args)

        {

            Demo demo = new Demo();



            Thread t = new Thread(new ThreadStart(demo.Run));

            t.Name = "NoParameterThread";

            t.Start();

        }

 

   public class Demo

    {

        int interval = 1000;

        /// <summary>

        /// 不带参数的启动方法

        /// </summary>

        public void Run()

        {

            for (int i = 0; i < 10; i++)

            {

                DoSomething();

            }

        }



        private void DoSomething()

        {

            Console.WriteLine(string.Format("当前线程:{0},当前系统时间为:{1}", Thread.CurrentThread.Name, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));

            Thread.Sleep(interval);

        }

}

2:带参数的启动方法,就要使用ParameterizedThreadStart委托来实例化Thread了,和ThreadStart一样的是它也是线程启动时要执行的方法,和ThreadStart不同的是,它在实例化时可以用一个带有一个Object参数的方法作为构造函数的参数,而实例化ThreadStart时所用到的方法是没有参数的。ParameterizedThreadStart定义为:

public delegate void ParameterizedThreadStart(object obj);

使用方法如下面的代码:

 public class Demo

    {

        int interval = 1000;



        private void DoSomething()

        {

            Console.WriteLine(string.Format("当前线程:{0},当前系统时间为:{1}", Thread.CurrentThread.Name, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));

            Thread.Sleep(interval);

        }



        /// <summary>

        /// 带参数的启动方法

        /// </summary>

        /// <param name="param"></param>

        public void Run(object param)

        {

            if (param == null)

                return;



            int.TryParse(param.ToString(), out interval);

            for (int i = 0; i < 10; i++)

            {

                DoSomething();

            }

        }



    }
static void Main(string[] args)

        {

            Demo demo = new Demo();



            Thread parameterThread = new Thread(new ParameterizedThreadStart(demo.Run));

            parameterThread.Name = "ParameterThread";

            parameterThread.Start(2000);

        }

3:在很多时候,我们遇到的情况是要传递多个参数,注意到ParameterizedThreadStart委托的参数类型是一个Object对象,为什么是Object这样的参数呢?很简单,因为在.net中Object是所有类型的基类。这样我们可以声明一个类,为这个类增加属性,这些属性也就是参数。

使用方法如下面的代码:

 static void Main(string[] args)

        {

            Demo demo = new Demo();



            ThreadParamter p = new ThreadParamter(2000,100);

            Thread multiParameterThread = new Thread(new ParameterizedThreadStart(demo.CustomerParamterRun));

            multiParameterThread.Name = "MultiParameterThread";

            multiParameterThread.Start(p);

        }
 public class Demo

    {

        /// <summary>

        /// 带多个参数的启动方法

        /// </summary>

        /// <param name="param"></param>

        public void CustomerParamterRun(object param)

        {

            if (param == null)

                return;



            ThreadParamter p = param as ThreadParamter;

            if (p != null)

            {

                for (int i = 0; i < p.LoopCount; i++)

                {

                    Console.WriteLine(string.Format("当前线程:{0},当前系统时间为:{1}", Thread.CurrentThread.Name, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));

                    Thread.Sleep(p.Interval);

                }

            }

        }

    }



    public class ThreadParamter

    {

        public int Interval { get; set; }

        public int LoopCount { get; set; }



        public ThreadParamter()

        { }



        public ThreadParamter(int interval, int loopCount)

        {

            this.Interval = interval;

            this.LoopCount = loopCount;

        }

    }

4:在遇到业务非常复杂的时候,上面写法还是有问题,封装不够好,我们可以使用装饰模式,对上面的代码进行改进。这样业务发生改变的时候,我们只需要修改核心的实现部分,调用的方法可以不用做任何修改,而且调用方法的代码非常简洁。

修改后的代码如下:

 static void Main(string[] args)

        {

            DecoratorThread t = new DecoratorThread(new ThreadParamter(2000, 100));

            t.Start();

        }
 public class ThreadParamter

    {

        public int Interval { get; set; }

        public int LoopCount { get; set; }



        public ThreadParamter()

        { }



        public ThreadParamter(int interval, int loopCount)

        {

            this.Interval = interval;

            this.LoopCount = loopCount;

        }

    }



    /// <summary>

    /// 使用装饰模式来实现多个参数的

    /// </summary>

    public class DecoratorThread

    {

        private ThreadParamter threadParamter;

        private Thread thread;





        public DecoratorThread(ThreadParamter threadParamter)

        {

            this.threadParamter = threadParamter;

            thread = new Thread(new ThreadStart(Run));

            thread.Name = "DecoratorThread";

        }



        public void Start()

        {

            if (thread != null)

            {

                thread.Start();

            }

        }



        private void Run()

        {

            for (int i = 0; i < threadParamter.LoopCount; i++)

            {

                Console.WriteLine(string.Format("当前线程:{0},当前系统时间为:{1}", Thread.CurrentThread.Name, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));

                Thread.Sleep(threadParamter.Interval);

            }

        }

    }

你可能感兴趣的:(C#)