异步编程(1)
我们知道,使用多线程可以提高程序运行的效率,加速程序的运行。但是我们也应该知道每个线程都要耗费许多资源,在程序中不是运行的线程越多好,我们要掌握如何充分利用多线程的优势。要尽量使线程运行,不要让它挂起,因为挂起的线程不再运行。但是仍然耗费系统资源。
线程池是一种非常好的技术,可以大大提高程序的效率,而且又把新建每个线程的消耗降到最小。下面我们看一下c#中提供的有关线程池的方法以及如何使用:
static Boolean QueueUserWorkItem(WaitCallback callBack);
static Boolean QueueUserWorkItem(WaitCallback callBack, Object state);
static Boolean UnsafeQueueUserWorkItem(WaitCallback callBack, Object state);
using System;
using System.Threading;
public static class Program {
public static void Main() {
Console.WriteLine("Main thread: queuing an asynchronous operation");
ThreadPool.QueueUserWorkItem(ComputeBoundOp, 5);
Console.WriteLine("Main thread: Doing other work here...");
Thread.Sleep(10000);
Console.WriteLine("Hit <Enter> to end this program...");
Console.ReadLine();
}
private static void ComputeBoundOp(Object state) {
Console.WriteLine("In ComputeBoundOp: state={0}", state);
Thread.Sleep(1000);
}
}
//有时候是这样的结果
Main thread: queuing an asynchronous operation
Main thread: Doing other work here...
In ComputeBoundOp: state=5
//有时候又是这样的结果
Main thread: queuing an asynchronous operation
In ComputeBoundOp: state=5
Main thread: Doing other work here...
//我们看出两个线程的调度是由系统决定的,我们是无法选择的。
static Boolean QueueUserWorkItem(WaitCallback callBack, Object state);
static Boolean UnsafeQueueUserWorkItem(WaitCallback callBack, Object state);
using System;
using System.Threading;
public static class Program {
public static void Main() {
Console.WriteLine("Main thread: queuing an asynchronous operation");
ThreadPool.QueueUserWorkItem(ComputeBoundOp, 5);
Console.WriteLine("Main thread: Doing other work here...");
Thread.Sleep(10000);
Console.WriteLine("Hit <Enter> to end this program...");
Console.ReadLine();
}
private static void ComputeBoundOp(Object state) {
Console.WriteLine("In ComputeBoundOp: state={0}", state);
Thread.Sleep(1000);
}
}
//有时候是这样的结果
Main thread: queuing an asynchronous operation
Main thread: Doing other work here...
In ComputeBoundOp: state=5
//有时候又是这样的结果
Main thread: queuing an asynchronous operation
In ComputeBoundOp: state=5
Main thread: Doing other work here...
//我们看出两个线程的调度是由系统决定的,我们是无法选择的。
下面再来看一下使用专用线程,我们知道如果程序只需要其他的1~2线程来辅助程序运行,这种情况下用线程池恐怕不是很适合,而使用专用线程比较好:
using System;
using System.Threading;
public static class Program {
public static void Main() {
Console.WriteLine("Main thread: starting a dedicated thread " +"to do an asynchronous operation");
Thread dedicatedThread = new Thread(ComputeBoundOp);
dedicatedThread.Start(5);
Console.WriteLine("Main thread: Doing other work here...");
Thread.Sleep(10000);
dedicatedThread.Join(); //等待线程终止
Console.WriteLine("Hit <Enter> to end this program...");
Console.ReadLine();
}
private static void ComputeBoundOp(Object state) {
Console.WriteLine("In ComputeBoundOp: state={0}", state);
Thread.Sleep(1000);
}
}
//同样是两种输出结果
Main thread: starting a dedicated thread to do an asynchronous operation
Main thread: Doing other work here...
In ComputeBoundOp: state=5
Main thread: starting a dedicated thread to do an asynchronous operation
In ComputeBoundOp: state=5
Main thread: Doing other work here...
using System.Threading;
public static class Program {
public static void Main() {
Console.WriteLine("Main thread: starting a dedicated thread " +"to do an asynchronous operation");
Thread dedicatedThread = new Thread(ComputeBoundOp);
dedicatedThread.Start(5);
Console.WriteLine("Main thread: Doing other work here...");
Thread.Sleep(10000);
dedicatedThread.Join(); //等待线程终止
Console.WriteLine("Hit <Enter> to end this program...");
Console.ReadLine();
}
private static void ComputeBoundOp(Object state) {
Console.WriteLine("In ComputeBoundOp: state={0}", state);
Thread.Sleep(1000);
}
}
//同样是两种输出结果
Main thread: starting a dedicated thread to do an asynchronous operation
Main thread: Doing other work here...
In ComputeBoundOp: state=5
Main thread: starting a dedicated thread to do an asynchronous operation
In ComputeBoundOp: state=5
Main thread: Doing other work here...
下面看一下定期执行某种操作我们应该如何使用定时器:
using System;
using System.Threading;
public static class Program {
public static void Main() {
Console.WriteLine("Main thread: starting a timer");
Timer t = new Timer(ComputeBoundOp, 5, 0, 2000);
Console.WriteLine("Main thread: Doing other work here...");
Thread.Sleep(10000);
t.Dispose();
}
private static void ComputeBoundOp(Object state) {
Console.WriteLine("In ComputeBoundOp: state={0}", state);
Thread.Sleep(1000); // Simulates other work (1 second)
}
}
using System.Threading;
public static class Program {
public static void Main() {
Console.WriteLine("Main thread: starting a timer");
Timer t = new Timer(ComputeBoundOp, 5, 0, 2000);
Console.WriteLine("Main thread: Doing other work here...");
Thread.Sleep(10000);
t.Dispose();
}
private static void ComputeBoundOp(Object state) {
Console.WriteLine("In ComputeBoundOp: state={0}", state);
Thread.Sleep(1000); // Simulates other work (1 second)
}
}
如果我们查看FCL的话,我们会看到三个Timer类,下面分别介绍一下他们:
System.Threading.Timer //这个定时器就是我们使用的那个,是最好的一个定时器,建议都使用这个
System.Windows.Forms.Timer
/*构建一个该类的实例可以告诉Windows将定时器与调用线程关联。随着定时器的触发,Windows将一个定时消息插入到线程的消息队列中。调用线程必须执行一个消息循环,从而提取消息,并分别将他们分派到希望的回调方法中。*/
System.Timers.Timer
/*该定时器基本上是对System.Threading.Timer 的一个封装,当定时器到期后,经导致FCL将事件加入线程池队列中。这个定时器有可能被移除,以便都使用System.Threading.Timer */
System.Windows.Forms.Timer
/*构建一个该类的实例可以告诉Windows将定时器与调用线程关联。随着定时器的触发,Windows将一个定时消息插入到线程的消息队列中。调用线程必须执行一个消息循环,从而提取消息,并分别将他们分派到希望的回调方法中。*/
System.Timers.Timer
/*该定时器基本上是对System.Threading.Timer 的一个封装,当定时器到期后,经导致FCL将事件加入线程池队列中。这个定时器有可能被移除,以便都使用System.Threading.Timer */