传递参数到线程的n个方法 【转载】

原文地址: http://www.cnblogs.com/moonz-wu/archive/2007/09/10/888688.html
最近在写搜索引擎方面的东西,涉及到性能方面的问题,目前主要考虑使用多线程来解决性能问题,故找到了这篇关于多线程方面的文章,觉得不错,故转载于此,供以后学习和查找用。
本片文章的议题是有关于传递参数到线程的几种方法。

首先我们要知道什么是线程,什么时候要用到线程,如何去使用线程,如何更好的利用线程来完成工作。
线程是程序可执行片段的最小单元,是组成运行时程序的基本单元,一个进程有至少一个线程组成。一般在并行处理等待事件的时候要用到线程,如等待网络响应,等待I/O通讯,后台事务处理等情况。使用线程其实很简单,在.net框架下面你首先要定义一个函数来完成一些工作,然后实例化一个线程对象Thread thrd = new Thread(new ThreadStart(线程函数)); 其中ThreadStart是一个不带参数的函数委托。最后使用thrd.Start()就可以启动线程了。当然这只是一个很简单的例子,实际中使用线程会有很多的问题要解决,比如传递参数到线程中,等待线程返回,如何同步线程进行同一资源访问,如何防止死锁和竞争条件,如何有效的利用线程池,如何提供线程效率等问题。本文在这里将只对传递参数到线程进行探讨。

在上面举例中我们看到线程实例化的参数是一个不带任何参数的函数委托,那么就证明了我们不可能通过这样一个委托传递参数到线程内部。那么我们该怎么做呢?就我目前所知有三种方法可以实现:1. 利用线程实现类,将调用参数定义成属性的方式来操作线程参数;2. 利用ParameterizedThreadStart委托来传递输入参数;3. 利用线程池来实现参数传入。

1. 创建线程实现类,这种方式有个最大的优点就是可以通过线程返回多个返回值
假设存在在一个打印功能的线程,通过主函数传入打印的行,列和字符数据,并返回打印的字符总数。
None.gif      class  ThreadOutput
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
int _rowCount = 0;
InBlock.gif        
int _colCount = 0;
InBlock.gif        
char _char = '*';
InBlock.gif        
int _ret = 0;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 输入参数
ExpandedSubBlockEnd.gif         
/// </summary>

InBlock.gif        public int RowCount
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _rowCount = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public int ColCount
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _colCount = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public char Character
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _char = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 输出参数
ExpandedSubBlockEnd.gif         
/// </summary>

InBlock.gif        public int RetVal
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _ret; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void Output()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
for (int row = 0; row < _rowCount; row++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
for (int col = 0; col < _colCount; col++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Console.Write(
"{0} ", _char);
InBlock.gif                    _ret
++;
ExpandedSubBlockEnd.gif                }

InBlock.gif                Console.Write(
"\n");
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gifdot.gif
InBlock.gif            ThreadOutput to1 
= new ThreadOutput();
InBlock.gif            to1.RowCount 
= 10;
InBlock.gif            to1.ColCount 
= 20;
InBlock.gif
InBlock.gif            Thread thrd 
= new Thread(new ThreadStart(to1.Output));
InBlock.gif            
// 设置为后台线程,主要是为不影响主线程的结束
InBlock.gif
            thrd.IsBackground = true;
InBlock.gif            thrd.Start();

2. 利用ParameterizedThreadStart委托来传递输入参数
ParameterizedThreadStart委托是.Net2.0中才有的。该委托提供来在启动线程时传递一个object参数到线程中。这种方式使用起来比较简单,但是由于需要对object对象进行类型转换,所以存在类型不一致的隐患。

3. 利用线程池来实现参数传入
什么是线程池,线程池是系统提供一个存放线程的容器,进入线程池后的线程控制权由系统掌握。利用线程池的好处是我们无需为线程存在大量空闲时间而去思考干点别的什么,适合于常规性的事务处理。在.Net中线程池是一个static类,所以我们需要通过ThreadPool来调用相关的函数。其中向线程池中加入线程的函数就是ThreadPool.QueueUserWorkItem(new WaitCallBack(), object obj)。这个函数有个WaitCallBack的委托,[( true)]
public delegate WaitCallback( state)
通过这个委托我们也可以传递参数到线程中。

其实还有一种方法可以传递参数到线程中,那就是通过回调函数来实现,只不过这种方法本质上和第一种通过类的数据成员来传递参数的机制一样。所以就不再细说来!

具体的实现可以参考下面的源代码:

全部源代码如下:(vs 2005编译通过)
 

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

namespace  UsingThread
{
    
struct  RowCol
    {
        
public   int  row;
        
public   int  col;
    };

    
class  ThreadOutput
    {
        
//  建立等待时间
         static   public  ManualResetEvent prompt  =   new  ManualResetEvent( false );

        
int  _rowCount  =   0 ;
        
int  _colCount  =   0 ;
        
char  _char  =   ' * ' ;
        
int  _ret  =   0 ;

        
/**/
        
///   <summary>
        
///  输入参数
        
///   </summary>
         public   int  RowCount
        {
            
set  { _rowCount  =  value; }
        }

        
public   int  ColCount
        {
            
set  { _colCount  =  value; }
        }

        
public   char  Character
        {
            
set  { _char  =  value; }
        }

        
/**/
        
///   <summary>
        
///  输出参数
        
///   </summary>
         public   int  RetVal
        {
            
get  {  return  _ret; }
        }

        
public   void  Output()
        {
            
for  ( int  row  =   0 ; row  <  _rowCount; row ++ )
            {
                
for  ( int  col  =   0 ; col  <  _colCount; col ++ )
                {
                    Console.Write(
" {0}  " , _char);
                    _ret
++ ;
                }
                Console.Write(
" \n " );
            }
            
//  激活事件
            prompt.Set();
        }

        
public   void  Output(Object rc)
        {
            RowCol rowCol 
=  (RowCol)rc;
            
for  ( int  i  =   0 ; i  <  rowCol.row; i ++ )
            {
                
for  ( int  j  =   0 ; j  <  rowCol.col; j ++ )
                    Console.Write(
" {0}  " , _char);
                Console.Write(
" \n " );
            }
        }
    }

    
class  Program
    {
        
static   void  Main( string [] args)
        {
            ThreadOutput to1 
=   new  ThreadOutput();
            to1.RowCount 
=   10 ;
            to1.ColCount 
=   20 ;

            Thread thrd 
=   new  Thread( new  ThreadStart(to1.Output));
            
//  设置为后台线程,主要是为不影响主线程的结束
            thrd.IsBackground  =   true ;
            thrd.Start();

            
//  建立事件等待
            ThreadOutput.prompt.WaitOne();

            Console.WriteLine(
" {0} " , to1.RetVal);

            ThreadOutput to2 
=   new  ThreadOutput();
            to2.RowCount 
=   5 ;
            to2.ColCount 
=   18 ;
            to2.Character 
=   ' ^ ' ;
            Thread thrds 
=   new  Thread( new  ThreadStart(to2.Output));
            thrds.IsBackground 
=   true ;
            thrds.Start();
            thrds.Join();

            Console.WriteLine(
" {0} " , to2.RetVal);

            
//  传递参数给线程的另一种实现
            RowCol rc  =   new  RowCol();
            rc.row 
=   12 ;
            rc.col 
=   13 ;
            to1.Character 
=   ' @ ' ;
            
if  (ThreadPool.QueueUserWorkItem( new  WaitCallback(to1.Output), rc))
                Console.WriteLine(
" Insert Pool is success! " );
            
else
                Console.WriteLine(
" Insert Pool is failed! " );
            Thread.Sleep(
1000 );


            
//  使用新的ThreadStart委托来传递参数
            rc.col  =   19 ;
            to2.Character 
=   ' # ' ;
            Thread thrdt 
=   new  Thread( new  ParameterizedThreadStart(to2.Output));
            thrdt.Start(rc);
            thrdt.Join();
            Console.ReadKey();
        }

    }
}

 
最后要注意的是由于线程实现类是通过属性来传递数值的,那么在属性访问器中要进行线程同步,否则取得的值可能不正确。

转载于:https://www.cnblogs.com/OceanChen/archive/2009/05/06/1450526.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值