[C#]『Barrier』任务并行库使用小计

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

Barrier  是一个对象,它可以在并行操作中的所有任务都达到相应的关卡之前,阻止各个任务继续执行。 如果并行操作是分阶段执行的,并且每一阶段要求各任务之间进行同步,则可以使用该对象。 ——MSDN

按照我的理解Barrier其实就是将多个任务同步,而同步需要一个屏障或者是关卡,那么其方法SignalAndWait()就是屏障的作用;

我们来模拟现实中例子,做火车就是很好的参照,大家知道,火车的车次有个发车点,到了那个时间点才能发车,那我们稍微修改下,人到齐后才能发车。

1.首先从家出发,在路上实现:

PRivatestaticvoidOnRoading(stringname,intcostTime)

{

Console.WriteLine(string.Format("[{0}]在去火车站路上.....,花费{1}小时.", name, costTime));

Thread.Sleep(newTimeSpan(0, 0, costTime));

}

2.达到火车站,等候火车,这相当于同步任务,(所模拟的是,乘火车到人到齐后才能发车)

privatestaticvoidOnStationing(stringname)

{

Console.WriteLine(string.Format("[{0}]到达火车站,正在安检等候火车.....", name));gate.SignalAndWait();}

.csharpcode, .csharpcode pre

{

font-size: small;

color: black;

font-family: consolas, "Courier New", courier, monospace;

background-color: #ffffff;

/*white-space: pre;*/

}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #006080; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp{ background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt

{

background-color: #f4f4f4;

width: 100%;

margin: 0em;

}

.csharpcode .lnum { color: #606060; }3.人到齐后,发车

privatestaticvoidOnTraining(stringname)

{

Console.WriteLine(string.Format("[{0}]乘坐火车离开.....", name));

}

完整代码实现

staticBarrier gate;staticvoidMain(string[] args)

{try{

ToStationWorkTyp2();

}catch(Exception ex)

{

Console.WriteLine(string.Format("Exception Message:{0}", ex.Message.Trim()));

}finally{

Console.ReadLine();

}

}privatestaticvoidToStationWorkTyp2()

{

gate =newBarrier(3);

Task _taskA = Task.Factory.StartNew(() =>

{

OnRoading("personA", 2);

OnStationing("personA");

OnTraining("personA");

});

Task _taskB = Task.Factory.StartNew(() =>

{

OnRoading("personB", 5);

OnStationing("personB");

OnTraining("personB");

});

Task _taskC = Task.Factory.StartNew(() =>

{

OnRoading("personC", 3);

OnStationing("personC");

OnTraining("personC");

});

Task.WaitAll(_taskA, _taskB, _taskC);

}privatestaticvoidOnTraining(stringname)

{

Console.WriteLine(string.Format("[{0}]乘坐火车离开.....", name));

}privatestaticvoidOnStationing(stringname)

{

Console.WriteLine(string.Format("[{0}]到达火车站,正在安检等候火车.....", name));

gate.SignalAndWait();

}privatestaticvoidOnRoading(stringname,intcostTime)

{

Console.WriteLine(string.Format("[{0}]在去火车站路上.....,花费{1}小时.", name, costTime));

Thread.Sleep(newTimeSpan(0, 0, costTime));

}

.csharpcode, .csharpcode pre

{

font-size: small;

color: black;

font-family: consolas, "Courier New", courier, monospace;

background-color: #ffffff;

/*white-space: pre;*/

}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #006080; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt

{

background-color: #f4f4f4;

width: 100%;

margin: 0em;

}

.csharpcode .lnum { color: #606060; }代码效果

另外一种代码实现

usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.Diagnostics;usingSystem.Linq;usingSystem.Threading;usingSystem.Threading.Tasks;namespacePLinq

{classProgram

{staticBarrier gate;staticvoidMain(string[] args)

{try{

ToStationWorkTyp1();

}catch(Exception ex)

{

Console.WriteLine(string.Format("Exception Message:{0}", ex.Message.Trim()));

}finally{

Console.ReadLine();

}

}privatestaticvoidToStationWorkTyp1()

{

gate =newBarrier(3);

Task _taskA = Task.Factory.StartNew(() => ToStation("PersonA", 2));

Task _taskB = Task.Factory.StartNew(() => ToStation("PersonB", 3));

Task _taskC = Task.Factory.StartNew(() => ToStation("PersonC", 5));

Task.WaitAll(_taskA, _taskB, _taskC);

}privatestaticvoidToStation(stringname,intcostTime)

{

Console.WriteLine(string.Format("[{0}]在去火车站路上.....,花费{1}小时.", name, costTime));

Thread.Sleep(newTimeSpan(0, 0, costTime));

Console.WriteLine(string.Format("[{0}]到达火车站,正在安检等候火车.....", name));

gate.SignalAndWait();

Console.WriteLine(string.Format("[{0}]乘坐火车离开.....", name));

}

}

}

.csharpcode, .csharpcode pre

{

font-size: small;

color: black;

font-family: consolas, "Courier New", courier, monospace;

background-color: #ffffff;

/*white-space: pre;*/

}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #006080; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt

{

background-color: #f4f4f4;

width: 100%;

margin: 0em;

}

.csharpcode .lnum { color: #606060; }同样的实现效果

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航