`
yimeng528
  • 浏览: 183868 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

第五节:Quzrtz 的 线程池

阅读更多

在说到Quzrtz线程池的时候要先讲一下线程池的概念 :

我个人的理解就是把要执行的东东扔到一个类似水池子的容器里面,给它洗澡,具体怎么洗,洗的干净不干净,还是一个个等着排队洗,都算是线程池对线程的管理,官方的概念也不想找了,就按这样理解吧。

百度上找了下有哪些情况下不使用线程池,感觉挺不错,贴出来看下:

●如果需要使一个任务具有特定优先级

●如果具有可能会长时间运行(并因此阻塞其他任务)的任务

●如果需要将线程放置到单线程单元中(线程池中的线程均处于多线程单元中)

●如果需要永久标识来标识和控制线程,比如想使用专用线程来终止该线程,将其挂起或按名称发现它

 

上面说了哪么多,下面来点正题,说说Quzrtz线程池:

Quzrtz的线程池很简单,它只需要实现IThreadPool接口就可以了,Quzrtz中执行任务会用过的线程池有:SimpleThreadPool这个,还有一个算的上没有用的吧:ZeroSizeThreadPool

平常Quzrtz的轮循线程发现有满足条件需要执行的job的时候,就会毫不留情的扔到SimpleThreadPool中洗一澡,下面让我们好好端详一下SimpleThreadPool这个类是如何工作的,先看下类图:

         其中图上的WorkerThread类是SimpleThreadPool类内部包裹的一个类。

 

IThreadRunnable

  这个接口只定义了一个方法:Run(),是执行任务的操作。

  

[csharp] view plaincopy
  1. /// <summary>  
  2.   /// This interface should be implemented by any class whose instances are intended   
  3.   /// to be executed by a thread.  
  4.   /// </summary>  
  5.   public interface IThreadRunnable  
  6.   {  
  7.       /// <summary>  
  8.       /// This method has to be implemented in order that starting of the thread causes the object's   
  9.       /// run method to be called in that separately executing thread.  
  10.       /// </summary>  
  11.       void Run();  
  12.   }  

 

QuartzThread:

  这是个关键的抽象类, WorkerThread就是它的子类,一个该类的实例就相当于一个线程,有自己的名字,有优先级,可以中断执行等。当执行它的Start方法时,就会启动一个线程:

  

[csharp] view plaincopy
  1. /// <summary>  
  2.     /// Support class used to handle threads  
  3.     /// </summary>  
  4.     /// <author>Marko Lahma (.NET)</author>  
  5.     public abstract class QuartzThread : IThreadRunnable  
  6.     {  
  7.         /// <summary>  
  8.         /// The instance of System.Threading.Thread  
  9.         /// </summary>  
  10.         private readonly Thread thread;  
  11.   
  12.         /// <summary>  
  13.         /// Initializes a new instance of the QuartzThread class  
  14.         /// </summary>  
  15.         protected QuartzThread()  
  16.         {  
  17.             thread = new Thread(Run);  
  18.         }  
  19.   
  20.         /// <summary>  
  21.         /// Initializes a new instance of the Thread class.  
  22.         /// </summary>  
  23.         /// <param name="name">The name of the thread</param>  
  24.         protected QuartzThread(string name)  
  25.         {  
  26.             thread = new Thread(Run);  
  27.             Name = name;  
  28.         }  
  29.   
  30.         /// <summary>  
  31.         /// This method has no functionality unless the method is overridden  
  32.         /// </summary>  
  33.         public virtual void Run()  
  34.         {  
  35.         }  
  36.   
  37.         /// <summary>  
  38.         /// Causes the operating system to change the state of the current thread instance to ThreadState.Running  
  39.         /// </summary>  
  40.         public void Start()  
  41.         {  
  42.             thread.Start();  
  43.         }  
  44.   
  45.         /// <summary>  
  46.         /// Interrupts a thread that is in the WaitSleepJoin thread state  
  47.         /// </summary>  
  48.         protected void Interrupt()  
  49.         {  
  50.             thread.Interrupt();  
  51.         }  
  52.   
  53.         /// <summary>  
  54.         /// Gets or sets the name of the thread  
  55.         /// </summary>  
  56.         public string Name  
  57.         {  
  58.             get { return thread.Name; }  
  59.             protected set  
  60.             {  
  61.                 thread.Name = value;  
  62.             }  
  63.         }  
  64.   
  65.         /// <summary>  
  66.         /// Gets or sets a value indicating the scheduling priority of a thread  
  67.         /// </summary>  
  68.         protected ThreadPriority Priority  
  69.         {  
  70.             get { return thread.Priority; }  
  71.             set { thread.Priority = value; }  
  72.         }  
  73.   
  74.         /// <summary>  
  75.         /// Gets or sets a value indicating whether or not a thread is a background thread.  
  76.         /// </summary>  
  77.         protected bool IsBackground  
  78.         {  
  79.             set { thread.IsBackground = value; }  
  80.         }  
  81.   
  82.         /// <summary>  
  83.         /// Blocks the calling thread until a thread terminates  
  84.         /// </summary>  
  85.         public void Join()  
  86.         {  
  87.             thread.Join();  
  88.         }  
  89.   
  90.         /// <summary>  
  91.         /// Obtain a string that represents the current object  
  92.         /// </summary>  
  93.         /// <returns>A string that represents the current object</returns>  
  94.         public override string ToString()  
  95.         {  
  96.             return string.Format(CultureInfo.InvariantCulture, "Thread[{0},{1},]", Name, Priority);  
  97.         }  
  98.     }  

WorkerThread

这个类可以理解也线程池中最小线程单元,一个线程启动的时候,如果用默认配置的话,会初始化十个WorkerThread,像它的父类,它当然是一个线程,它重写了run方法,而且核心代码都写在了这个方法中,如果要读源码的时候,一定要看两眼。代码见源码。

SimpleThreadPool

线程池类,负责对WorkerThread调度管理,比如有一个job来了,它要把这个job分配给哪个空闲的WorkerThread去处理,处理完了,要把这个WorkerThread置成空闲的状态,以便下次继续分配执行job,这里执行的job都需要继承IThreadRunnable接口

 

比如,我现在写一个job 就继承IThreadRunnable,如何让SimpleThreadPool来完成这个job的执行:

 1.如下面自己定义的job

[csharp] view plaincopy
  1. public class JobDemo : IThreadRunnable  
  2.     {  
  3.         public void Run()  
  4.         {  
  5.             Console.WriteLine("JobDemo正在运行,运行时间是" + DateTime.Now + Thread.CurrentThread.Name);  
  6.         }  
  7.     }  

 

2.让简单线程池去执行我的job

 

[csharp] view plaincopy
  1. static void Main(string[] args)  
  2.         {  
  3.             //实例化我要执行的job  
  4.             JobDemo demo = new JobDemo();  
  5.   
  6.             //实例线程池,线程数为10  
  7.             SimpleThreadPool simpleThreadPool = new SimpleThreadPool(10, ThreadPriority.Normal);  
  8.             //初始化线程池,这步必须要有  
  9.             simpleThreadPool.Initialize();  
  10.   
  11.             //如果有空闲的线程,就执行任务.  
  12.             //如果simpleThreadPool.BlockForAvailableThreads()内部没有可用的线程时,会处于阻塞状态  
  13.             //理论上simpleThreadPool.BlockForAvailableThreads() > 0 永远为true  
  14.             while (simpleThreadPool.BlockForAvailableThreads() > 0)  
  15.             {  
  16.                 simpleThreadPool.RunInThread(demo);  
  17.                 Thread.Sleep(500);  
  18.             }  
  19.   
  20.             Console.Read();  
  21.   
  22.             //干掉所有的线程  
  23.             simpleThreadPool.Shutdown();  
  24.         }  

 

3.结果:

 

整个代码,已经从Quartz中剥离出来,下载点我

http://download.csdn.net/detail/wanggang421338916/4190504

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics