.NET多线程编程
线程的基本概念
• 线程是程序执行的基本原子单位. 一个进程可以由多个线程组成.
• 每个线程都维护异常处理程序、调度优先级和一组系统用于在调度该线程前保存线程上下文的结构。线程上下文包括为使线程在线程的宿主进程地址空间中无缝地继续执行所需的所有信息,包括线程的CPU 寄存器组和堆栈。
• 在分布式编程中,正确使用线程能够很好的提高应用程序的性能及运行效率.实现原理是将一个进程分成多个线程,然后让它们并发异步执行,来提高运行效率.
• 并发执行并不是同时执行(占有CPU),任意时刻还是只能有一个线程占用CPU,只不过是它们争夺CPU频繁一些,感觉到他们似乎都在运行.
什么时候用线程?
• 一般情况下,如果多个线程在执行时都要抢占某一个资源或某几个资源,则最好不用异步线程执行.因为它们是并发执行,很可能同时争夺某个资源有CPU,这时要么执行资源分配算法(比如要判断哪个线程优先级高,这要花费时间),或者是按时间片算法(这样要付出轮询CUP/交接/让出CPU所需的时间).
• 如果多个线程所需要的系统资源是比较均匀的,这时完全可以让它们异步并发执行,
使用线程的缺点
• 统将为进程和线程所需的上下文信息使用内存。因此,可以创建的进程、AppDomain 对象和线程的数目会受到可用内存的限制。
• 跟踪大量的线程将占用大量的处理器时间。如果线程过多,则其中大多数线程都不会产生明显的进度。如果大多数当前线程处于一个进程中,则其他进程中的线程的调度频率就会很低。
• 使用许多线程控制代码执行非常复杂,并可能产生许多错误。
• 销毁线程需要了解可能发生的问题并对那些问题进行处理。
线程池
• 许多应用程序创建的线程都要在休眠状态中消耗大量时间,以等待事件发生。这样会浪费资源。
• 线程池通过为应用程序提供一个由系统管理的辅助线程池使您可以更为有效地使用线程。一个线程监视排到线程池的若干个等待操作的状态。当一个等待操作完成时,线程池中的一个辅助线程就会执行对应的回调函数。
• 实际上,如果要执行一些需要多个线程的较短任务,则使用ThreadPool 类是利用多个线程的最方便且最好的方法。使用线程池使系统能够不仅针对此进程而且针对计算机上的其他进程(您的应用程序对其一无所知)对此情况进行优化以达到更好的吞吐量。使用线程池使系统能够在考虑到计算机上的所有当前进程后对线程时间片进行优化。
ThreadPool
• 线程池在首次创建ThreadPool 类的实例时被创建。线程池具有每个可用处理器25 个线程的默认限制
• 可以将与等待操作不相关的工作项排列到线程池。若要请求由线程池中的一个线程来处理工作项,请调用QueueUserWorkItem 方法。此方法将对将被从线程池中选定的线程调用的方法或委托的引用用作参数。一个工作项排入队列后就无法再取消它。
创建和终止线程
using System;
using System.Threading;
public class Worker
{
// 启动线程时调用此方法。
public void DoWork()
{
while (!_shouldStop)
{
Console.WriteLine("worker thread: working...");
}
Console.WriteLine("worker thread: terminating gracefully.");
}
public void RequestStop()
{
_shouldStop = true;
}
// Volatile 用于向编译器提示此数据
// 成员将由多个线程访问。
private volatile bool _shouldStop;
}
public class WorkerThreadExample
{
static void Main()
{
// 创建线程对象。这不会启动该线程。
Worker workerObject = new Worker();
Thread workerThread = new Thread(workerObject.DoWork);
// 启动辅助线程。
workerThread.Start();
Console.WriteLine("main thread: Starting worker thread...");
// 循环直至辅助线程激活。
while (!workerThread.IsAlive);
// 为主线程设置 1 毫秒的休眠,
// 以使辅助线程完成某项工作。
Thread.Sleep(1);
// 请求辅助线程自行停止:
workerObject.RequestStop();
// 使用 Join 方法阻塞当前线程,
// 直至对象的线程终止。
workerThread.Join();
Console.WriteLine("main thread: Worker thread has terminated.");
}
}
使用线程池
using System;
using System.Threading;
// Fibonacci 类为使用辅助
// 线程执行长时间的 Fibonacci(N) 计算提供了一个接口。
// N 是为 Fibonacci 构造函数提供的,此外还提供了
// 操作完成时对象发出的事件信号。
// 然后,可以使用 FibOfN 属性来检索结果。
public class Fibonacci
{
public Fibonacci(int n, ManualResetEvent doneEvent)
{
_n = n;
_doneEvent = doneEvent;
}
// 供线程池使用的包装方法。
public void ThreadPoolCallback(Object threadContext)
{
int threadIndex = (int)threadContext;
Console.WriteLine("thread {0} started...", threadIndex);
_fibOfN = Calculate(_n);
Console.WriteLine("thread {0} result calculated...", threadIndex);
_doneEvent.Set();
}
// 计算第 N 个斐波纳契数的递归方法。
public int Calculate(int n)
{
if (n <= 1)
{
return n;
}
else
{
return Calculate(n - 1) + Calculate(n - 2);
}
}
public int N { get { return _n; } }
private int _n;
public int FibOfN { get { return _fibOfN; } }
private int _fibOfN;
ManualResetEvent _doneEvent;
}
public class ThreadPoolExample
{
static void Main()
{
const int FibonacciCalculations = 10;
// 每个 Fibonacci 对象使用一个事件
ManualResetEvent[] doneEvents = new ManualResetEvent[FibonacciCalculations];
Fibonacci[] fibArray = new Fibonacci[FibonacciCalculations];
Random r = new Random();
// 使用 ThreadPool 配置和启动线程:
Console.WriteLine("launching {0} tasks...", FibonacciCalculations);
for (int i = 0; i < FibonacciCalculations; i++)
{
doneEvents[i] = new ManualResetEvent(false);
Fibonacci f = new Fibonacci(r.Next(20, 40), doneEvents[i]);
fibArray[i] = f;
ThreadPool.QueueUserWorkItem(f.ThreadPoolCallback, i);
}
// 等待池中的所有线程执行计算...
WaitHandle.WaitAll(doneEvents);
Console.WriteLine("Calculations complete.");
// 显示结果...
for (int i = 0; i < FibonacciCalculations; i++)
{
Fibonacci f = fibArray[i];
Console.WriteLine("Fibonacci({0}) = {1}", f.N, f.FibOfN);
}
}
}
线程同步和互交
using System;
using System.Threading;
using System.Collections;
using System.Collections.Generic;
// 将线程同步事件封装在此类中,
// 以便于将这些事件传递给 Consumer 和
// Producer 类。
public class SyncEvents
{
public SyncEvents()
{
// AutoResetEvent 用于“新项”事件,因为
// 我们希望每当使用者线程响应此事件时,
// 此事件就会自动重置。
_newItemEvent = new AutoResetEvent(false);
// ManualResetEvent 用于“退出”事件,因为
// 我们希望发出此事件的信号时有多个线程响应。
// 如果使用 AutoResetEvent,事件
// 对象将在单个线程作出响应之后恢复为
// 未发信号的状态,而其他线程将
// 无法终止。
_exitThreadEvent = new ManualResetEvent(false);
// 这两个事件也放在一个 WaitHandle 数组中,以便
// 使用者线程可以使用 WaitAny 方法
// 阻塞这两个事件。
_eventArray = new WaitHandle[2];
_eventArray[0] = _newItemEvent;
_eventArray[1] = _exitThreadEvent;
}
// 公共属性允许对事件进行安全访问。
public EventWaitHandle ExitThreadEvent
{
get { return _exitThreadEvent; }
}
public EventWaitHandle NewItemEvent
{
get { return _newItemEvent; }
}
public WaitHandle[] EventArray
{
get { return _eventArray; }
}
private EventWaitHandle _newItemEvent;
private EventWaitHandle _exitThreadEvent;
private WaitHandle[] _eventArray;
}
// Producer 类(使用一个辅助线程)
// 将项异步添加到队列中,共添加 20 个项。
public class Producer
{
public Producer(Queue<int> q, SyncEvents e)
{
_queue = q;
_syncEvents = e;
}
public void ThreadRun()
{
int count = 0;
Random r = new Random();
while (!_syncEvents.ExitThreadEvent.WaitOne(0, false))
{
lock (((ICollection)_queue).SyncRoot)
{
while (_queue.Count < 20)
{
_queue.Enqueue(r.Next(0, 100));
_syncEvents.NewItemEvent.Set();
count++;
}
}
}
Console.WriteLine("Producer thread: produced {0} items", count);
}
private Queue<int> _queue;
private SyncEvents _syncEvents;
}
// Consumer 类通过自己的辅助线程使用队列
// 中的项。Producer 类使用 NewItemEvent
// 将新项通知 Consumer 类。
public class Consumer
{
public Consumer(Queue<int> q, SyncEvents e)
{
_queue = q;
_syncEvents = e;
}
public void ThreadRun()
{
int count = 0;
while (WaitHandle.WaitAny(_syncEvents.EventArray) != 1)
{
lock (((ICollection)_queue).SyncRoot)
{
int item = _queue.Dequeue();
}
count++;
}
Console.WriteLine("Consumer Thread: consumed {0} items", count);
}
private Queue<int> _queue;
private SyncEvents _syncEvents;
}
public class ThreadSyncSample
{
private static void ShowQueueContents(Queue<int> q)
{
// 对集合进行枚举本来就不是线程安全的,
// 因此在整个枚举过程中锁定集合以防止
// 使用者和制造者线程修改内容
// 是绝对必要的。(此方法仅由
// 主线程调用。)
lock (((ICollection)q).SyncRoot)
{
foreach (int i in q)
{
Console.Write("{0} ", i);
}
}
Console.WriteLine();
}
static void Main()
{
// 配置结构,该结构包含线程同步
// 所需的事件信息。
SyncEvents syncEvents = new SyncEvents();
// 泛型队列集合用于存储要制造和使用的
// 项。此例中使用的是“int”。
Queue<int> queue = new Queue<int>();
// 创建对象,一个用于制造项,一个用于
// 使用项。将队列和线程同步事件传递给
// 这两个对象。
Console.WriteLine("Configuring worker threads...");
Producer producer = new Producer(queue, syncEvents);
Consumer consumer = new Consumer(queue, syncEvents);
// 为制造者对象和使用者对象创建线程
// 对象。此步骤并不创建或启动
// 实际线程。
Thread producerThread = new Thread(producer.ThreadRun);
Thread consumerThread = new Thread(consumer.ThreadRun);
// 创建和启动两个线程。
Console.WriteLine("Launching producer and consumer threads...");
producerThread.Start();
consumerThread.Start();
// 为制造者线程和使用者线程设置 10 秒的运行时间。
// 使用主线程(执行此方法的线程)
// 每隔 2.5 秒显示一次队列内容。
for (int i = 0; i < 4; i++)
{
Thread.Sleep(2500);
ShowQueueContents(queue);
}
// 向使用者线程和制造者线程发出终止信号。
// 这两个线程都会响应,由于 ExitThreadEvent 是
// 手动重置的事件,因此除非显式重置,否则将保持“设置”。
Console.WriteLine("Signaling threads to terminate...");
syncEvents.ExitThreadEvent.Set();
// 使用 Join 阻塞主线程,首先阻塞到制造者线程
// 终止,然后阻塞到使用者线程终止。
Console.WriteLine("main thread waiting for threads to finish...");
producerThread.Join();
consumerThread.Join();
}
}
浙公网安备 33010602011771号