PS:前段时间稍微有些累,故暂停了本系列文章,今天状态开始恢复,继续旅程。

    之前几章已经整体把CCR介绍了一下,为了更好地了解和使用CCR,接下来几章我将逐一介绍CCR内重要的几个类,本章要介绍的是CCR的心脏Dispatcher。

    在我们使用Dispatcher的时候,最常用到的就是他的构造函数和资源释放函数了,因此本节,先介绍这2类函数,其他的函数因为涉及到DispatcherQueue等内部的运作机制,属于CCR内部的运作调度机制,留待前面这些基础知识交代完毕后再另起一篇细说。

Dispatcher内含的方法、属性预览
public sealed class Dispatcher : IDisposable
{
    
// 1、构造函数
    public Dispatcher();
    
public Dispatcher(int threadCount, string threadPoolName);
    
public Dispatcher(int threadCount, ThreadPriority priority,
            
bool useBackgroundThreads, string threadPoolName);
    
public Dispatcher(int threadCount, ThreadPriority priority,
            DispatcherOptions options, 
string threadPoolName);
    
public Dispatcher(int threadCount, ThreadPriority priority,
            DispatcherOptions options,
            ApartmentState threadApartmentState,
            
string threadPoolName);

    
// 2、资源释放函数
    public void Dispose();

    
// 3、属性
    public static ICollection<Causality> ActiveCausalities { get; }
    
public List<DispatcherQueue> DispatcherQueues { get; }
    
public string Name { getset; }
    
public DispatcherOptions Options { getset; }
    
public int PendingTaskCount { getset; }
    
public long ProcessedTaskCount { getset; }
    
public static int ThreadsPerCpu { getset; }
    
public int WorkerThreadCount { getset; }

    
// 4、因果关系函数
    public static void AddCausality(Causality causality);
    
public static void AddCausalityBreak();
    
public static void ClearCausalities();
    
public static bool RemoveCausality(Causality causality);
    
public static bool RemoveCausality(string name);
}

一、构造函数
     Dispatcher给我们提供了5个构造函数,其实是为了方便大家使用而对一个构造函数做的封装,我们看看具体的实现代码(我在关键的地方都加上了中文的注释):

}

二、Dispose函数
    使用该函数才能够释放Dispatcher内创建的线程池等资源,记得在退出程序之前,调用它,内部的实现细节很简单,详细请参考代码。

}

三、几个重要的枚举
1、 ThreadPriority指定Thread 的调度优先级。

public enum ThreadPriority
{
    
// 可以将 System.Threading.Thread 安排在具有任何其他优先级的线程之后。
    Lowest = 0,
    
// 可以将 System.Threading.Thread 安排在具有 Normal 优先级的线程之后,
    
// 在具有 Lowest 优先级的线程之前。
    BelowNormal = 1,
    
// 可以将 System.Threading.Thread 安排在具有 AboveNormal 优先级的线程之后,
    
// 在具有 BelowNormal 优先级的线程之前。默认情况下,线程具有
    
// Normal 优先级。
    Normal = 2,
    
// 可以将 System.Threading.Thread 安排在具有 Highest 优先级的线程之后,
    
// 在具有 Normal 优先级的线程之前。
    AboveNormal = 3,
    
// 可以将 System.Threading.Thread 安排在具有任何其他优先级的线程之前。
    Highest = 4,
}

2、DispatcherOptions: 线程池运行选项

public enum DispatcherOptions
{
    None,
    
/// <summary>
    
/// 时候后台线程
    
/// </summary>
    UseBackgroundThreads,
    
/// <summary>
    
/// 按照CPU来分配线程
    
/// </summary>
    UseProcessorAffinity
}

3、ApartmentState: 指定 System.Threading.Thread 的单元状态

public enum ApartmentState
{
    
// System.Threading.Thread 将创建并进入一个单线程单元。
    STA = 0,
    
// System.Threading.Thread 将创建并进入一个多线程单元。
    MTA = 1,
    
// 尚未设置 System.Threading.Thread.ApartmentState 属性。
    Unknown = 2,
}

四、附录:
1、相关参考链接
MSDN杂志上的一个CCR专栏文章:http://msdn.microsoft.com/zh-cn/magazine/cc163556(en-us).aspx

2、本系列其他文章
我的CCR之旅(3):CCR任务调度的原理和策略

相关文章:

  • 2021-12-31
  • 2022-03-02
  • 2021-11-06
  • 2022-01-07
  • 2021-10-13
  • 2021-06-23
  • 2021-06-07
  • 2021-04-05
猜你喜欢
  • 2022-03-06
  • 2021-08-23
  • 2022-12-23
  • 2021-10-02
  • 2022-12-23
  • 2021-12-18
相关资源
相似解决方案