.Net并行编程之同步机制

时间:2022-10-27 12:34:13

 一:Barrier(屏障同步)

二:spinLock(自旋锁)

信号量

 一:CountdownEvent

  虽然通过Task.WaitAll()方法也可以达到线程同步的目的。

  但是CountdownEvent更牛X之处在于我们可以动态的改变“信号计数”的大小,比如一会儿能够容纳8个线程,一下又4个,一下又10个,CountdownEvent给我们提供了可以动态修改的解决方案。

模拟建房子工序

   public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} static CountdownEvent cde = null;
private void button1_Click(object sender, EventArgs e)
{
//建房子第一期工人
string[] _bhPerson1 = new string[] { "Yan", "Zhi", "wei", "Do", "Work" };
//建房子第二期工人
string[] _bhPerson2 = new string[] { "Yan2", "Zhi2", "wei2" };
//建房子第三期工人
string[] _bhPerson3 = new string[] { "Yan3", "Zhi3", "wei3" };
using (cde = new CountdownEvent(Environment.ProcessorCount))//开始监管,相当于监工
{
cde.Reset(_bhPerson1.Length);//设置第一期建造需要5个人
foreach (string person in _bhPerson1)
{
Task.Factory.StartNew(() =>
{
BuilderHourseStep1(person);
}); }
cde.Wait();//等待第一期建造完成
Console.WriteLine("-----------------------"); cde.Reset(_bhPerson2.Length);//设置第二期建需要三个人
foreach (string person in _bhPerson2)
{
Task.Factory.StartNew(() =>
{
BuilderHourseStep2(person);
});
}
cde.Wait();//等待第二期建造完成
Console.WriteLine("-----------------------"); cde.Reset(_bhPerson3.Length);//设置第三期建需要三个人
foreach (string person in _bhPerson3)
{
Task.Factory.StartNew(() =>
{
BuilderHourseStep3(person);
});
}
cde.Wait();//等待第三期建造完成
Console.WriteLine("-----------------------");
}
}
/// <summary>
/// 建房子第一道所需要的工序
/// </summary>
/// <param name="person"></param>
static void BuilderHourseStep1(string person)
{
try
{
Console.WriteLine(string.Format("『{0}』BuilderHourseStep1....", person));
}
finally
{
cde.Signal();//建造完成一点后,通知监工
}
}
/// <summary>
/// 建房子第二道所需要的工序
/// </summary>
/// <param name="person"></param>
static void BuilderHourseStep2(string person)
{
try
{
Console.WriteLine(string.Format("『{0}』BuilderHourseStep2.....", person));
}
finally
{
cde.Signal();
}
}
/// <summary>
/// 建房子第三道所需要的工序
/// </summary>
/// <param name="person"></param>
static void BuilderHourseStep3(string person)
{
try
{
Console.WriteLine(string.Format("『{0}』BuilderHourseStep3.......", person));
}
finally
{
cde.Signal();
}
}
}

分步执行任务

  static void Main(string[] args)
{
IList<string> taskList =new List<string> {"任务一","任务二", "任务四", "任务五", "任务六" };
int count = taskList.Count;
using (CountdownEvent cde = new CountdownEvent(Environment.ProcessorCount))
{
cde.Reset(count);//设置信号数
foreach (var item in taskList)
{
Task.Factory.StartNew(() =>
{
Console.WriteLine("处理任务:"+item);
cde.Signal();//每次调用 Signal 时,信号计数都会递减 1。
});
}
cde.Wait();// 在主线程上,对 Wait 的调用将会阻塞,直至信号计数为零。
Console.WriteLine("接下来开始做下一件事情");
cde.Reset(count);//重置信号数
foreach (var item in taskList)
{
Task.Factory.StartNew(() =>
{
Console.WriteLine("继续处理任务:" + item);
cde.Signal();//每次调用 Signal 时,信号计数都会递减 1。
});
}
}
Console.ReadKey();
}

 二:SemaphoreSlim

三: ManualResetEventSlim

教程

http://blog.gkarch.com/threading/part5.html#the-parallel-class

http://www.cnblogs.com/huangxincheng/archive/2012/04/08/2437701.html

http://www.cnblogs.com/yaopengfei/p/8315212.html