在说到Quzrtz线程池的时候要先讲一下线程池的概念 :
我个人的理解就是把要执行的东东扔到一个类似水池子的容器里面,给它洗澡,具体怎么洗,洗的干净不干净,还是一个个等着排队洗,都算是线程池对线程的管理,官方的概念也不想找了,就按这样理解吧。
百度上找了下有哪些情况下不使用线程池,感觉挺不错,贴出来看下:
●如果需要使一个任务具有特定优先级
●如果具有可能会长时间运行(并因此阻塞其他任务)的任务
●如果需要将线程放置到单线程单元中(线程池中的线程均处于多线程单元中)
●如果需要永久标识来标识和控制线程,比如想使用专用线程来终止该线程,将其挂起或按名称发现它
上面说了哪么多,下面来点正题,说说Quzrtz的线程池:
Quzrtz的线程池很简单,它只需要实现IThreadPool接口就可以了,Quzrtz中执行任务会用过的线程池有:SimpleThreadPool这个,还有一个算的上没有用的吧:ZeroSizeThreadPool。
平常Quzrtz的轮循线程发现有满足条件需要执行的job的时候,就会毫不留情的扔到SimpleThreadPool中洗一澡,下面让我们好好端详一下SimpleThreadPool这个类是如何工作的,先看下类图:
其中图上的WorkerThread类是SimpleThreadPool类内部包裹的一个类。
IThreadRunnable:
这个接口只定义了一个方法:Run(),是执行任务的操作。
- /// <summary>
- /// This interface should be implemented by any class whose instances are intended
- /// to be executed by a thread.
- /// </summary>
- public interface IThreadRunnable
- {
- /// <summary>
- /// This method has to be implemented in order that starting of the thread causes the object's
- /// run method to be called in that separately executing thread.
- /// </summary>
- void Run();
- }
QuartzThread类:
这是个关键的抽象类, WorkerThread就是它的子类,一个该类的实例就相当于一个线程,有自己的名字,有优先级,可以中断执行等。当执行它的Start方法时,就会启动一个线程:
- /// <summary>
- /// Support class used to handle threads
- /// </summary>
- /// <author>Marko Lahma (.NET)</author>
- public abstract class QuartzThread : IThreadRunnable
- {
- /// <summary>
- /// The instance of System.Threading.Thread
- /// </summary>
- private readonly Thread thread;
- /// <summary>
- /// Initializes a new instance of the QuartzThread class
- /// </summary>
- protected QuartzThread()
- {
- thread = new Thread(Run);
- }
- /// <summary>
- /// Initializes a new instance of the Thread class.
- /// </summary>
- /// <param name="name">The name of the thread</param>
- protected QuartzThread(string name)
- {
- thread = new Thread(Run);
- Name = name;
- }
- /// <summary>
- /// This method has no functionality unless the method is overridden
- /// </summary>
- public virtual void Run()
- {
- }
- /// <summary>
- /// Causes the operating system to change the state of the current thread instance to ThreadState.Running
- /// </summary>
- public void Start()
- {
- thread.Start();
- }
- /// <summary>
- /// Interrupts a thread that is in the WaitSleepJoin thread state
- /// </summary>
- protected void Interrupt()
- {
- thread.Interrupt();
- }
- /// <summary>
- /// Gets or sets the name of the thread
- /// </summary>
- public string Name
- {
- get { return thread.Name; }
- protected set
- {
- thread.Name = value;
- }
- }
- /// <summary>
- /// Gets or sets a value indicating the scheduling priority of a thread
- /// </summary>
- protected ThreadPriority Priority
- {
- get { return thread.Priority; }
- set { thread.Priority = value; }
- }
- /// <summary>
- /// Gets or sets a value indicating whether or not a thread is a background thread.
- /// </summary>
- protected bool IsBackground
- {
- set { thread.IsBackground = value; }
- }
- /// <summary>
- /// Blocks the calling thread until a thread terminates
- /// </summary>
- public void Join()
- {
- thread.Join();
- }
- /// <summary>
- /// Obtain a string that represents the current object
- /// </summary>
- /// <returns>A string that represents the current object</returns>
- public override string ToString()
- {
- return string.Format(CultureInfo.InvariantCulture, "Thread[{0},{1},]", Name, Priority);
- }
- }
WorkerThread:
这个类可以理解也线程池中最小线程单元,一个线程启动的时候,如果用默认配置的话,会初始化十个WorkerThread,像它的父类,它当然是一个线程,它重写了run方法,而且核心代码都写在了这个方法中,如果要读源码的时候,一定要看两眼。代码见源码。
SimpleThreadPool:
线程池类,负责对WorkerThread调度管理,比如有一个job来了,它要把这个job分配给哪个空闲的WorkerThread去处理,处理完了,要把这个WorkerThread置成空闲的状态,以便下次继续分配执行job,这里执行的job都需要继承IThreadRunnable接口
比如,我现在写一个job 就继承IThreadRunnable,如何让SimpleThreadPool来完成这个job的执行:
1.如下面自己定义的job
- public class JobDemo : IThreadRunnable
- {
- public void Run()
- {
- Console.WriteLine("JobDemo正在运行,运行时间是" + DateTime.Now + Thread.CurrentThread.Name);
- }
- }
2.让简单线程池去执行我的job:
- static void Main(string[] args)
- {
- //实例化我要执行的job
- JobDemo demo = new JobDemo();
- //实例线程池,线程数为10
- SimpleThreadPool simpleThreadPool = new SimpleThreadPool(10, ThreadPriority.Normal);
- //初始化线程池,这步必须要有
- simpleThreadPool.Initialize();
- //如果有空闲的线程,就执行任务.
- //如果simpleThreadPool.BlockForAvailableThreads()内部没有可用的线程时,会处于阻塞状态
- //理论上simpleThreadPool.BlockForAvailableThreads() > 0 永远为true
- while (simpleThreadPool.BlockForAvailableThreads() > 0)
- {
- simpleThreadPool.RunInThread(demo);
- Thread.Sleep(500);
- }
- Console.Read();
- //干掉所有的线程
- simpleThreadPool.Shutdown();
- }
3.结果:
整个代码,已经从Quartz中剥离出来,下载点我。