浅谈C#中的多线程编程[2]

这次复习线程编程中涉及到的编程Thread的静态方法与属性,先看一段代码:
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);
		}
	}
}


   执行的结果如下图:
2011030919035133.png 

  未完待续,下次复习多线程编程中Timer定时器的使用。 

转载于:https://www.cnblogs.com/wpdev/archive/2011/03/09/1978668.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值