文档及代码下载:http://www.dingos.cn/index.php?topic=805.0
介绍
线程是轻量级的进程。使用线程能提供应用系统的效率。为了使用多线程需要引入System命名空间中的Threading命名空间。System.Threading命名空间包含需要使用多线程的地方。现在让我们来看第一个程序。
Program 1
using System;
using System.Threading;
public class MyThread {
public static void Thread1() {
for (int i = 0; i < 10; i++) {
Console.WriteLine("Thread1 {0}", i);
}
}
public static void Thread2() {
for (int i = 0; i < 10; i++) {
Console.WriteLine("Thread2 {0}", i);
}
}
}
public class MyClass {
public static void Main() {
Console.WriteLine("Before start thread");
Thread tid1 = new Thread(new ThreadStart(MyThread.Thread1));
Thread tid2 = new Thread(new ThreadStart(MyThread.Thread2));
tid1.Start();
tid2.Start();
}
}
开始探索整个程序。
这个程序有一个MyThread类包括两个静态方法Thread1和Thread2。为了创建线程需要创建Thread类对象。Thread类的构造方法是ThreadStart的引用。这个构造方法可能会发出两种类型的异常;当参数是一个空引用时引发ArgumentNullException异常或当程序不允许创建线程的SecurityException异常。
Thread类的参数是ThreadStart的引用。当线程开始时执行ThreadStart指向的方法。ThreadStart的参数名是函数名,作为线程的函数。Thread1是一个静态方法可以使用类名来访问不需要要创建类的对象。线程开始执行Thread类的Start()方法。下面是程序的输出:
运行结果
Before start thread
Thread1 0
Thread1 1
Thread1 2
Thread1 3
Thread1 4
Thread1 5
Thread1 6
Thread1 7
Thread1 8
Thread1 9
Thread2 0
Thread2 1
Thread2 2
Thread2 3
Thread2 4
Thread2 5
Thread2 6
Thread2 7
Thread2 8
Thread2 9
线程的方法可以不是静态方法。在下面的例子中使用非静态方法需要创建类的对象。
Program 2
using System;
using System.Threading;
public class MyThread {
public void Thread1() {
for (int i = 0; i < 10; i++) {
Console.WriteLine("Thread1 {0}", i);
}
}
public void Thread2() {
for (int i = 0; i < 10; i++) {
Console.WriteLine("Thread2 {0}", i);
}
}
}
public class MyClass {
public static void Main() {
Console.WriteLine("Before start thread");
MyThread thr = new MyThread();
Thread tid1 = new Thread(new ThreadStart(thr.Thread1));
Thread tid2 = new Thread(new ThreadStart(thr.Thread2));
tid1.Start();
tid2.Start();
}
}
这个程序的输出和上一个的输出相同。
不需要为两个线程创建两个方法。可以使用一个方法为两个线程类创建两个线程。看下面的程序。
Program 3
using System;
using System.Threading;
public class MyThread {
public void Thread1() {
for (int i = 0; i < 10; i++) {
Console.WriteLine("Hello world {0}", i);
}
}
}
public class MyClass {
public static void Main() {
Console.WriteLine("Before start thread");
MyThread thr1 = new MyThread();
MyThread thr2 = new MyThread();
Thread tid1 = new Thread(new ThreadStart(thr1.Thread1));
Thread tid2 = new Thread(new ThreadStart(thr2.Thread1));
tid1.Start();
tid2.Start();
}
}
这里创建了两个MyThread对象执行两个线程。
在构造方法传递参数时也不需要创建ThreadStart对象。可以创建ThreadStart对象并将它传递给Thread参数。看下面的程序。
Program 4
using System;
using System.Threading;
public class MyThread {
public void Thread1() {
for (int i = 0; i < 10; i++) {
Console.WriteLine("Hello world {0}", i);
}
}
}
public class MyClass {
public static void Main() {
Console.WriteLine("Before start thread");
MyThread thr1 = new MyThread();
MyThread thr2 = new MyThread();
ThreadStart tStart1 = new ThreadStart(thr1.Thread1);
ThreadStart tStart2 = new ThreadStart(thr2.Thread1);
Thread tid1 = new Thread(tStart1);
Thread tid2 = new Thread(tStart2);
tid1.Start();
tid2.Start();
}
}
这个程序也有相同的输出。
Sleep方法在指定的时间范围内挂起线程。Sleep是静态方法使用时不需要创建Thread线程。有两个重载的Sleep()方法,一个是指定时间的毫秒数,另一个是TimeSpan结构的实例。
Program 5
using System;
using System.Threading;
public class MyThread {
public void Thread1() {
for (int i = 0; i < 10; i++) {
Console.WriteLine("Hello world " + i);
Thread.Sleep(1);
}
}
}
public class MyClass {
public static void Main() {
Console.WriteLine("Before start thread");
MyThread thr1 = new MyThread();
MyThread thr2 = new MyThread();
Thread tid1 = new Thread(new ThreadStart(thr1.Thread1) );
Thread tid2 = new Thread(new ThreadStart(thr2.Thread1) );
tid1.Start();
tid2.Start();
}
}
这个线程休眠1秒并给第二个线程一个机会执行。下面是输出结果:
Before start thread
Hello world 0
Hello world 0
Hello world 1
Hello world 1
Hello world 2
Hello world 2
Hello world 3
Hello world 3
Hello world 4
Hello world 4
Hello world 5
Hello world 5
Hello world 6
Hello world 6
Hello world 7
Hello world 7
Hello world 8
Hello world 8
Hello world 9
Hello world 9
看似这两个线程是并行执行的。下面程序将显示重载的Sleeep方法。
Program 6
using System;
using System.Threading;
public class MyThread {
public void Thread1() {
for (int i = 0; i < 10; i++) {
int iHour = 0;
int iMin = 0;
int iSec = 1;
Console.WriteLine("Hello world " + i);
Thread.Sleep(new TimeSpan(iHour, iMin, iSec) );
}
}
}
public class MyClass {
public static void Main() {
Console.WriteLine("Before start thread");
MyThread thr1 = new MyThread();
MyThread thr2 = new MyThread();
Thread tid1 = new Thread(new ThreadStart(thr1.Thread1) );
Thread tid2 = new Thread(new ThreadStart(thr2.Thread1) );
tid1.Start();
tid2.Start();
}
}
TimeSpan结构有四个重载的构造方法。第一个是参数为长整型的指定特殊值,第二个有3个整型参数分别表示时、分、秒,第3个有4个整型参数分别表示日、时、分、秒,第4个构造方法有5 个参数表示日、时、分、秒和毫秒。这个程序和上面的程序打印一秒钟打印一次新值外其他的相同。
Sleep方法抛出3中类型的异常:当时间值小于0时将引发ArgumentException异常,当休眠线程中断时引发ThreadInterruptedException异常,当不适当的时候调用方法将引发SecurityException异常。
下面程序显示执行情况下Sleep()方法是负面的。
Program 7
using System;
using System.Threading;
public class MyThread {
public void Thread1() {
for (int i = 0; i < 10; i++) {
int iHour = 0;
int iMin = 0;
int iSec = -1;
try {
Console.WriteLine("Hello world " + i);
Thread.Sleep(new TimeSpan(iHour, iMin, iSec) );
} catch (ArgumentException ae) {
Console.WriteLine(ae.Message );
}
}
}
}
public class MyClass {
public static void Main() {
Console.WriteLine("Before start thread");
MyThread thr1 = new MyThread();
MyThread thr2 = new MyThread();
Thread tid1 = new Thread(new ThreadStart(thr1.Thread1) );
Thread tid2 = new Thread(new ThreadStart(thr2.Thread1) );
tid1.Start();
tid2.Start();
}
}
Message是ArgumentException类的一个属性用来描述异常信息。在这个程序中会给出如下错误信息。
Parameter Name: Argument must be greater than 0 and less than 2^31 - 1milliseconds.