C#多线程编程(1):线程的启动

 在实例化Thread的实例,需要提供一个委托,在实例化这个委托时所用到的参数是线程将来启动时要运行的方法。在.net中提供了两种启动线程的方式,一种是不带参数的启动方式,另一种是带参数的启动的方式。
 不带参数的启动方式
 如果启动参数时无需其它额外的信息,可以使用ThreadStart来实例化Thread,如下面的代码:
 
InBlock.gif using System;
InBlock.gif using System.Collections.Generic;
InBlock.gif using System.Text;
InBlock.gif using System.Threading;
InBlock.gif    
InBlock.gif namespace StartThread
InBlock.gif {
InBlock.gif         class Program
InBlock.gif         {
InBlock.gif                 int interval = 200;
InBlock.gif                 static void Main( string[] args)
InBlock.gif                 {
InBlock.gif                         Program p = new Program();
InBlock.gif                         Thread nonParameterThread = new Thread( new ThreadStart(p.NonParameterRun));
InBlock.gif                         nonParameterThread.Start();
InBlock.gif                 }
InBlock.gif                 /// <summary>
InBlock.gif                 /// 不带参数的启动方法
InBlock.gif                 /// </summary>
InBlock.gif                 public void NonParameterRun()
InBlock.gif                 {
InBlock.gif                         for ( int i = 0; i < 10; i++)
InBlock.gif                         {
InBlock.gif                                 Console.WriteLine( "系统当前时间毫秒值:"+DateTime.Now.Millisecond.ToString());
InBlock.gif                                 Thread.Sleep(interval); //让线程暂停
InBlock.gif                         }
InBlock.gif         }
InBlock.gif }

 程序的运行效果我们不用运行也会知道,那就是在循环中将系统当前时间的毫秒部分输出出来,在每次输出之后会将当前线程暂停一下,直到10次之后运行完毕,终止线程的执行。
 在上面的代码中我们是通过定义全局变量的方法来指定线程暂停间隔,按照这种方法,假如要运行10个线程,每个线程的暂停间隔不一样的话,就需要定义10个全局变量,虽然最终不影响系统的运行效果,但是总觉得不是太爽。
 有没有比较简单一点的办法呢?有!那就是使用带参数的启动方法。
  带参数的启动方法
 如果要在实例化线程时要带一些参数,就不能用ThreadStart委托作为构造函数的参数来实例化Thread了,而要ParameterizedThreadStart委托,和ThreadStart一样的是它也是线程启动时要执行的方法,和ThreadStart不同的是,它在实例化时可以用一个带有一个Object参数的方法作为构造函数的参数,而实例化ThreadStart时所用到的方法是没有参数的。
 为什么是Object这样的参数呢?很简单,因为在.net中Object是所有类型的基类,用它可以表示Array(数组)、Interface(接口)、ValueType(值类型,如bool,byte,char,short,int,float,long,double等)、class(类)等.net中的类型。当然,这也意味着如果你要启动一个线程,给它传递一个int类型参数时,必须在启动方法中进行相应的类型转换。
 下面就是一个例子,在启动线程时指定了线程的暂停间隔,代码如下:
 
InBlock.gif using System;
InBlock.gif using System.Collections.Generic;
InBlock.gif using System.Text;
InBlock.gif using System.Threading;
InBlock.gif    
InBlock.gif namespace StartThread
InBlock.gif {
InBlock.gif         class Program
InBlock.gif         {
InBlock.gif                 int interval = 200;
InBlock.gif                 static void Main( string[] args)
InBlock.gif                 {
InBlock.gif                         Program p = new Program();
InBlock.gif    
InBlock.gif                         Thread parameterThread = new Thread( new ParameterizedThreadStart(p.ParameterRun));
InBlock.gif                         parameterThread.Name = "Thread A:";
InBlock.gif                         parameterThread.Start(30);
InBlock.gif                 }
InBlock.gif        
InBlock.gif                 /// <summary>
InBlock.gif                 /// 带参数的启动方法
InBlock.gif                 /// </summary>
InBlock.gif                 /// <param name="ms">让线程在运行过程中的休眠间隔</param>
InBlock.gif                 public void ParameterRun( object ms)
InBlock.gif                 {
InBlock.gif                         int j = 10;
InBlock.gif                         int.TryParse(ms.ToString(), out j); //这里采用了TryParse方法,避免不能转换时出现异常
InBlock.gif                         for ( int i = 0; i < 10; i++)
InBlock.gif                         {
InBlock.gif                                 Console.WriteLine(Thread.CurrentThread.Name+ "系统当前时间毫秒值:" + DateTime.Now.Millisecond.ToString());
InBlock.gif                                 Thread.Sleep(j); //让线程暂停
InBlock.gif                         }
InBlock.gif                 }
InBlock.gif         }
InBlock.gif }

 在这个方法里,我们在启动线程时顺便指定了线程的暂停间隔,也就是这句:
 parameterThread.Start(30);
 线程启动时运行的方法是public void ParameterRun(object ms),这个值为30的int类型变量被装箱成object,所以在方法中还需要将它转换成int类型,这个可以通过拆箱或者其它办法解决。
 假如我们要启动两个线程,每个线程的暂停间隔不一样,启动代码如下:
InBlock.gif using System;
InBlock.gif using System.Collections.Generic;
InBlock.gif using System.Text;
InBlock.gif using System.Threading;
InBlock.gif    
InBlock.gif namespace StartThread
InBlock.gif {
InBlock.gif         class Program
InBlock.gif         {
InBlock.gif                 int interval = 200;
InBlock.gif                 static void Main( string[] args)
InBlock.gif                 {
InBlock.gif                         Program p = new Program();
InBlock.gif    
InBlock.gif                         Thread parameterThread = new Thread( new ParameterizedThreadStart(p.ParameterRun));
InBlock.gif                         parameterThread.Name = "Thread A:";
InBlock.gif                         parameterThread.Start(30);
InBlock.gif                         //启动第二个线程
InBlock.gif                         parameterThread = new Thread( new ParameterizedThreadStart(p.ParameterRun));
InBlock.gif                         parameterThread.Name = "Thread B:";
InBlock.gif                         parameterThread.Start(60);
InBlock.gif                 }
InBlock.gif        
InBlock.gif                 /// <summary>
InBlock.gif                 /// 带参数的启动方法
InBlock.gif                 /// </summary>
InBlock.gif                 /// <param name="ms">让线程在运行过程中的休眠间隔</param>
InBlock.gif                 public void ParameterRun( object ms)
InBlock.gif                 {
InBlock.gif                         int j = 10;
InBlock.gif                         int.TryParse(ms.ToString(), out j); //这里采用了TryParse方法,避免不能转换时出现异常
InBlock.gif                         for ( int i = 0; i < 10; i++)
InBlock.gif                         {
InBlock.gif                                 Console.WriteLine(Thread.CurrentThread.Name+ "系统当前时间毫秒值:" + DateTime.Now.Millisecond.ToString());
InBlock.gif                                 Thread.Sleep(j); //让线程暂停
InBlock.gif                         }
InBlock.gif                 }
InBlock.gif         }
InBlock.gif }

  对上面的代码做一点说明,就是线程启动之后,线程的实例不必再存在,例如在上面的代码中我用的是同一个实例实例化了两个线程,并且这两个线程运行很正常。
 
  继续探索
 上面解决了一个问题,如果在启动线程时需要参数如何解决,如果针对上面的问题继续发掘,比如:在启动线程时不但要指定线程的暂停间隔,还需要指定循环次数(在上面的所有例子中都是执行10次的),这个问题该如何解决呢?
 有两种办法可以解决:
 首先可以继续在ParameterizedThreadStart这里做文章,因为这里可以使用一个Object类型的参数,那么可以通过数组或者一个类来解决(因为它们都是Object的子类)。我在做某个系统时确实采用数组处理过这种情况,这样就要求在线程启动方法中必须清楚知道数组中每个参数的用途,不是太方便。
 这里说说重新定义一个实体类来解决的方法,代码如下。
 
InBlock.gif using System;
InBlock.gif using System.Collections.Generic;
InBlock.gif using System.Text;
InBlock.gif using System.Threading;
InBlock.gif    
InBlock.gif namespace StartThread
InBlock.gif {
InBlock.gif         class MyThreadParameter
InBlock.gif         {
InBlock.gif                 private int interval;
InBlock.gif                 private int loopCount;
InBlock.gif                 /// <summary>
InBlock.gif                 /// 循环次数
InBlock.gif                 /// </summary>
InBlock.gif                 public int LoopCount
InBlock.gif                 {
InBlock.gif                         get { return loopCount; }
InBlock.gif                 }
InBlock.gif    
InBlock.gif                 /// <summary>
InBlock.gif                 /// 线程的暂停间隔
InBlock.gif                 /// </summary>
InBlock.gif                 public int Interval
InBlock.gif                 {
InBlock.gif                         get { return interval; }
InBlock.gif                 }
InBlock.gif             /// <summary>
InBlock.gif             /// 构造函数
InBlock.gif             /// </summary>
InBlock.gif                 /// <param name="interval">线程的暂停间隔</param>
InBlock.gif                 /// <param name="loopCount">循环次数</param>
InBlock.gif                 public MyThreadParameter( int interval, int loopCount)
InBlock.gif                 {
InBlock.gif                         this.interval = interval;
InBlock.gif                         this.loopCount = loopCount;
InBlock.gif                 }
InBlock.gif         }
InBlock.gif         class Program
InBlock.gif         {
InBlock.gif                 int interval = 200;
InBlock.gif                 static void Main( string[] args)
InBlock.gif                 {
InBlock.gif                         Program p = new Program();
InBlock.gif    
InBlock.gif                         Thread parameterThread = new Thread( new ParameterizedThreadStart(p.MyParameterRun));
InBlock.gif                         parameterThread.Name = "Thread A:";
InBlock.gif                         MyThreadParameter paramter = new MyThreadParameter(50, 20);
InBlock.gif                         parameterThread.Start(paramter);
InBlock.gif                 }
InBlock.gif    
InBlock.gif    
InBlock.gif                 /// <summary>
InBlock.gif                 /// 带多个参数的启动方法
InBlock.gif                 /// </summary>
InBlock.gif                 /// <param name="ms">方法参数</param>
InBlock.gif                 public void MyParameterRun( object ms)
InBlock.gif                 {
InBlock.gif                         MyThreadParameter parameter = ms as MyThreadParameter; //类型转换
InBlock.gif                         if (parameter != null)
InBlock.gif                         {
InBlock.gif                                 for ( int i = 0; i < parameter.LoopCount; i++)
InBlock.gif                                 {
InBlock.gif                                         Console.WriteLine(Thread.CurrentThread.Name + "系统当前时间毫秒值:" + DateTime.Now.Millisecond.ToString());
InBlock.gif                                         Thread.Sleep(parameter.Interval); //让线程暂停
InBlock.gif                                 }
InBlock.gif                         }
InBlock.gif                 }
InBlock.gif         }
InBlock.gif }
 第二种方法和上面方法有些相似,也是需要引入外部类,并且将Thread实例放在引入的类中,这种情况适合于在线程中处理的业务逻辑比较复杂的情况。在前不久处理的一个项目中我用过这种情况,它是用来实现双向数据传输的。
 如果实现上面的效果,代码如下:
 
InBlock.gif using System;
InBlock.gif using System.Collections.Generic;
InBlock.gif using System.Text;
InBlock.gif using System.Threading;
InBlock.gif    
InBlock.gif namespace StartThread
InBlock.gif {
InBlock.gif         class MyThreadParameter
InBlock.gif         {
InBlock.gif                 private int interval;
InBlock.gif                 private int loopCount;
InBlock.gif                 private Thread thread;
InBlock.gif                    
InBlock.gif     /// <summary>
InBlock.gif     /// 构造函数
InBlock.gif     /// </summary>
InBlock.gif                 /// <param name="interval">线程的暂停间隔</param>
InBlock.gif                 /// <param name="loopCount">循环次数</param>
InBlock.gif                 public MyThreadParameter( int interval, int loopCount)
InBlock.gif                 {
InBlock.gif                         this.interval = interval;
InBlock.gif                         this.loopCount = loopCount;
InBlock.gif                         thread = new Thread( new ThreadStart(Run));
InBlock.gif                 }
InBlock.gif    
InBlock.gif                 public void Start()
InBlock.gif                 {
InBlock.gif                         if (thread != null)
InBlock.gif                         {
InBlock.gif                                 thread.Start();
InBlock.gif                         }
InBlock.gif                 }
InBlock.gif    
InBlock.gif                 private void Run()
InBlock.gif                 {
InBlock.gif                         for ( int i = 0; i < loopCount; i++)
InBlock.gif                         {
InBlock.gif                                 Console.WriteLine( "系统当前时间毫秒值:" + DateTime.Now.Millisecond.ToString());
InBlock.gif                                 Thread.Sleep(interval); //让线程暂停
InBlock.gif                         }
InBlock.gif                 }
InBlock.gif         }
InBlock.gif         class Program
InBlock.gif         {
InBlock.gif                 static void Main( string[] args)
InBlock.gif                 {
InBlock.gif                         MyThreadParameter parameterThread = new MyThreadParameter(30, 50);
InBlock.gif                         parameterThread.Start();
InBlock.gif                 }
InBlock.gif    
InBlock.gif         }
InBlock.gif }

 上面的代码的运行效果和前面的代码运行效果类似,只不过是将业务处理代码放在一个单独的类MyThreadParameter中,使得MyThreadParameter看起来也像一个Thread,实际上维护的还是其内部的Thread,在一些大型系统中这样做的好处是便于维护。
 
 总结:在本篇主要讲述如何启动线程的问题,在启动时可能会遇到无需参数、需要多个参数的情况,在这里讲述了如何解决这些问题的思路。在.net类库中虽然存在着庞大的类库,但是并不是总会有合适的类来解决我们所遇到的问题,但是只要肯动脑筋总会想到合适的办法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值