首先我们要知道什么是线程,什么时候要用到线程,如何去使用线程,如何更好的利用线程来完成工作。
线程是程序可执行片段的最小单元,是组成运行时程序的基本单元,一个进程有至少一个线程组成。一般在并行处理等待事件的时候要用到线程,如等待网络响应,等待I/O通讯,后台事务处理等情况。使用线程其实很简单,在.net框架下面你首先要定义一个函数来完成一些工作,然后实例化一个线程对象Thread thrd = new Thread(new ThreadStart(线程函数)); 其中ThreadStart是一个不带参数的函数委托。最后使用thrd.Start()就可以启动线程了。当然这只是一个很简单的例子,实际中使用线程会有很多的问题要解决,比如传递参数到线程中,等待线程返回,如何同步线程进行同一资源访问,如何防止死锁和竞争条件,如何有效的利用线程池,如何提供线程效率等问题。本文在这里将只对传递参数到线程进行探讨。
在上面举例中我们看到线程实例化的参数是一个不带任何参数的函数委托,那么就证明了我们不可能通过这样一个委托传递参数到线程内部。那么我们该怎么做呢?就我目前所知有三种方法可以实现:1. 利用线程实现类,将调用参数定义成属性的方式来操作线程参数;2. 利用ParameterizedThreadStart委托来传递输入参数;3. 利用线程池来实现参数传入。
1. 创建线程实现类,这种方式有个最大的优点就是可以通过线程返回多个返回值
假设存在在一个打印功能的线程,通过主函数传入打印的行,列和字符数据,并返回打印的字符总数。
{
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");
}
}
ThreadOutput to1 = new ThreadOutput();
to1.RowCount = 10;
to1.ColCount = 20;
Thread thrd = new Thread(new ThreadStart(to1.Output));
// 设置为后台线程,主要是为不影响主线程的结束
thrd.IsBackground = true;
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();
}
}
}