浅谈C#中的多线程编程[2]
using System; using System.Threading; namespace ThreadTest { public class ClassSample { public void Method() { while (true) { Console.WriteLine("ClassSample中的Mehtod正在执行。"); } } } class Program { public static void Main(string[] args) { ClassSample classOne= new ClassSample(); Thread threadSample = new Thread(new ThreadStart(classOne.Method)); threadSample.Start(); while(!threadSample.IsAlive) Thread.Sleep(1); threadSample.Abort(); threadSample.Join(); Console.WriteLine(); Console.WriteLine("Class1中的Method1已经执行完毕。"); try { Console.WriteLine("尝试重新运行Class1的Method线程。"); threadSample.Start(); }catch(ThreadStateException) { Console.WriteLine("重新运行失败。"); Console.WriteLine(); } Console.ReadKey(true); } } }
注意到Thread threadSample = new Thread(new ThreadStart(classOne.Method));,所以当threadSample启动时,执行的是classOne中的Method,具体的执行流程是,在Main函数中的while循环中,使用了静态方法Thead.Sleep()让主线程休眠了一毫秒,从而执行线程threadSample。
随后执行ThreadSample.Abort()方法终止线程threadSample,在try循环中调用threadSample.Start()方法想重新启动线程,但由于Abort()方法终止线程是不可恢复的,所以会抛出异常,程序执行完毕。
----------------------------------------------------------知识分割线----------------------------------------------------------------------
①首先看到代码中的Main函数,所有线程都是依附于Main()函数所在的线程的,Main()函数是C#程序的入口,起始线程可以称之为主线程。如果所有的前台线程都停止了,那么主线程可以终止,而所有的后台线程都将无条件终止。所有的线程虽然在微观上是串行执行的,但是在宏观上你完全可以认为它们在并行执行。
②我们还需要了解Thread类的ThreadState属性,这个属性表示线程运行的状态,其取值有
Aborted:线程停止运行;
AbortRequested:Thread.Abort()方法已被调用,但线程还没有停止;
Background:线程在后台执行,与属性Thread.IsBackground有关;
Running:线程正在正常运行;
Stopped:线程已经被停止;
StopRequested:线程正在被要求停止;
Suspended:线程已经被挂起(此状态下,可以通过调用Resume()方法重新运行);
SuspendRequested:线程正在要求被挂起,但是未来得及响应;
Unstarted:未调用Thread.Start()开始线程的运行;
WaitSleepJoin:线程因为调用了Wait(),Sleep()或Join()等方法处于封锁状态;
上面提到了Background状态表示该线程在后台运行,那么后台运行的线程有什么特别的地方呢?其实后台线程跟前台线程只有一个区别,那就是后台线程不妨碍程序的终止。一旦一个进程所有的前台线程都终止后,CLR(通用语言运行环境)将通过调用任意一个存活中的后台进程的Abort()方法来彻底终止进程。
③最后我也要了解Thread的优先级,当CPU给不同的线程分配运行时间时,CPU是按照线程的优先级给予分配的,在C#中,线程拥有五个不同的优先级,由高到低分别是Highest、AboveNormal、Normal、BelowNormal和Lowest,可以通过下面的语句指定线程的优先级:
threadSample.Priority = ThreadPriority.Hightest;
下面的代码分别演示了上面提到的各种属性:
using System; using System.Threading; namespace ThreadTest { public class ClassSample { int i; public void MethodSample() { i++; Console.WriteLine("ClassSample中的MehtodSample正在执行,此时i的值为:{0}",i); } } class Program { public static void Main(string[] args) { ClassSample classOne = new ClassSample(); Thread threadOne = new Thread(new ThreadStart(classOne.MethodSample)); threadOne.Start(); Thread.Sleep(1000); //主线程休眠1秒,执行MethodSample方法 Console.WriteLine("threadOne此时的运行状态为:{0}",threadOne.ThreadState); threadOne.Abort(); Thread threadTwo = new Thread(new ThreadStart(classOne.MethodSample)); //Console.WriteLine("threadOne此时的运行状态为:{0}",threadOne.ThreadState); //如果threadTwo还没有启动,那么ThreadOne的状态应该是AbortRequested threadTwo.Start(); Console.WriteLine("threadOne此时的运行状态为:{0}",threadOne.ThreadState); Console.WriteLine("threadTwo此时的优先级是:{0}",threadTwo.Priority); threadTwo.Priority=ThreadPriority.Highest; Console.WriteLine("threadTwo此时的优先级是:{0}",threadTwo.Priority); Console.ReadKey(true); } } }
执行的结果如下图:
未完待续,下次复习多线程编程中Timer定时器的使用。
@:卿之 → http://www.cnblogs.com/wpdev
©:博文是本人当时的学习笔记及知识整理,由于自身局限错误在所难免,敬请斧正.
©:本文版权属于博客园和本人,版权基于署名 2.5 中国大陆许可协议发布,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接和署名卿之(包含链接),不得删节,否则保留追究法律责任的权利。