十六、Barrier类(*)

对于同步,Barrier类非常适用于其中工作有多个任务分支且以后又需要合并工作的情况。Barrier类用于需要同步的参与者。激活一个任务时,就可以动态地添加其他参与者,例如,从父任务中创建子任务。参与者在继续之前,可以等待所有其他参与者完成其工作。

BarrierSample有点复杂,但它展示了Barrier类型的功能。下面的应用程序使用一个包含2 000 000个随机字符串的集合。使用多个任务遍历该集合,并统计以a、b、c等开头的字符串个数。工作不仅分布在不同的任务之间,也放在一个任务中。毕竟所有的任务都迭代字符串的第一个集合,汇总结果,以后任务会继续处理下一个集合。

FillData()方法创建一个集合,并用随机字符串填充它:

        static IEnumerable<string> FillData(int size)
        {
            var random = new Random();
            return Enumerable.Range(0, size).Select(x => GetString(random));
        }
        static string GetString(Random random)
        {
            var strBuilder = new StringBuilder(6);
            for (int i = 0; i < 6; i++)
            {
                strBuilder.Append((char)(random.Next(26)+97));
            }
            return strBuilder.ToString();
        }

在LogBarrierInformation方法中定义一个辅助方法,来显示Barrier的信息:

        static void LogBarrierInformation(string info,Barrier barrier)
        {
            Console.WriteLine($"Task {Task.CurrentId}: {info}." +
                $"{barrier.ParticipantCount} current and " +
                $"{barrier.ParticipantsRemaining} remaining participants," +
                $"phase {barrier.CurrentPhaseNumber}");
        }

CalculationInTask()方法定义了任务执行的作业。通过参数,第3个参数引用Barrier实例。用于计算的数据是数组IList<string>[]。最后一个参数是int锯齿数组,用于在任务执行过程中写出结果。

任务把处理放在一个循环中。每一次循环中,都处理IList<string>[]的数组元素。每个循环完成后,任务通过调用SignalAndWait()方法,发出做好了准备的信号,并等待,直到所有的其他任务也准备好处理为止。这个循环会继续执行,直到任务完全完成为止。接着,任务就会使用RemoveParticipant()方法从Barrier类中删除它自己:

        static void CalculatorInTask(int jobNumber,int partitionSize,Barrier barrier,
            IList<string>[] stringArray,int loops,int[][] results)
        {
            LogBarrierInformation("CalculatorInTask started",barrier);
            for (int i = 0; i < 5; i++)
            {
                var data = new List<string>(stringArray[i]);
                int start = jobNumber * partitionSize;
                int end = start + partitionSize;
                Console.WriteLine($"Task {Task.CurrentId} in loop {i}: partition from {start} to {end}");
                for (int j = start; j < end; j++)
                {
                    var str = data[j];
                    char c = str[0];
                    var result = results[i]; //results是一个results[5][]锯齿数组, results[i]是一个int[26]数组
                    result[c-97]++; //如果c = 'a',result[97-97]++ = result[0]++,即代表a出现了一次,int[0]的值加1
                }
                Console.WriteLine($"Calculation completed from task {Task.CurrentId}" +
                    $"int loop {i}. {results[i][0]} tiems a,{results[i][25]} times z");
                LogBarrierInformation("sending signal and wait for all", barrier);
                barrier.SignalAndWait();
                LogBarrierInformation("waiting completed", barrier);
            }            
            barrier.RemoveParticipant();
            LogBarrierInformation("finished task,removed participant",barrier);
        }

在Main()方法中创建一个Barrier实例。在构造函数中,可以指定参与者的数量。在该示例中,这个数量是3(numberTasks+1),因为该示例创建了两个任务,Main()方法本身也是一个参与者。使用TaskRun()创建两个任务,把遍历集合的任务分为两个部分。启动该任务后,使用SignalAndWait()方法,Main()方法在完成时发出信号,并等待所有其他参与者或者发出完成的信号,或者从Barrier类中删除它们。一旦所有的参与者都准备好,就提取任务的结果,并使用Zip()扩展方法把它们合并起来。接着进行下一次迭代,等待任务的下一个结果:

        static void Main(string[] args)
        {
            const int numberTasks = 2;
            const int partitionSize = 1000000;
            const int loops = 5;
            var taskResults = new Dictionary<int, int[][]>();
            var data = new List<string>[loops];
            for (int i = 0; i < loops; i++)
            {
                data[i] = new List<string>(FillData(partitionSize * numberTasks));
            }
            var barrier = new Barrier(numberTasks + 1);
            LogBarrierInformation("initial participants in barrier", barrier);
            for (int i = 0; i < numberTasks; i++)
            {
                barrier.AddParticipant();
                int jobNumber = i;
                taskResults.Add(i, new int[loops][]);
                for (int loop = 0; loop < loops; loop++)
                {
                    taskResults[i][loop] = new int[26];
                }
                Console.WriteLine($"Main - starting task job {jobNumber}");
                Task.Run(() => CalculatorInTask(jobNumber, partitionSize, barrier, data, loops, taskResults[jobNumber]));
            }
            for (int loop = 0; loop < 5; loop++)
            {
                LogBarrierInformation("main task, start signaling and wait", barrier);
                barrier.SignalAndWait();
                LogBarrierInformation("main task waiting completed", barrier);
                int[][] resultCollection1 = taskResults[0];
                int[][] resultCollection2 = taskResults[1];
                var resultCollection = resultCollection1[loop].Zip(resultCollection2[loop], (c1, c2) => c1 + c2);
                char ch = 'a';
                int sum = 0;
                foreach (var x in resultCollection)
                {
                    Console.WriteLine($"{ch++}, cout: {x}");
                    sum += x;
                }
                LogBarrierInformation($"main task finished loop {loop}, sum: {sum}", barrier);
                Console.WriteLine("finished all iterations");
            }
            Console.ReadKey();
        }

运行应用程序,输出如下所示。在输出中可以看到,每个AddParticipant调用都会增加参与者的数量和剩下的参与者数量。只要一个参与者调用SignalAndWait,剩下的参与者数就会递减。当剩下的参与者数量达到0时,所有参与者的等待就结束,开始下一个阶段:

Task : initial participants in barrier.3 current and 3 remaining participants,phase 0
Main - starting task job 0
Main - starting task job 1
Task : main task, start signaling and wait.5 current and 5 remaining participants,phase 0
Task 2: CalculatorInTask started.5 current and 4 remaining participants,phase 0
Task 1: CalculatorInTask started.5 current and 4 remaining participants,phase 0
Task 2 in loop 0: partition from 1000000 to 2000000
Task 1 in loop 0: partition from 0 to 1000000
Calculation completed from task 2int loop 0. 38482 tiems a,38596 times z
Task 2: sending signal and wait for all.5 current and 4 remaining participants,phase 0
Calculation completed from task 1int loop 0. 38520 tiems a,38462 times z
Task 1: sending signal and wait for all.5 current and 3 remaining participants,phase 0

注意:作者示例代码有问题,并未得到想要的结果。

修改后的完整代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static System.Console;

namespace Event信号
{
    class Program
    {
        static void Main(string[] args)
        {
            const int numberTasks = 2;
            const int partitionSize = 1000000;
            const int loops = 5;
            var taskResults = new Dictionary<int, int[][]>();
            var data = new List<string>[loops];
            for (int i = 0; i < loops; i++)
            {
                data[i] = new List<string>(FillData(partitionSize * numberTasks));
            }
            var barrier = new Barrier(numberTasks + 1);
            LogBarrierInformation("initial participants in barrier", barrier);
            for (int i = 0; i < numberTasks; i++)
            {
                //barrier.AddParticipant();
                int jobNumber = i;
                taskResults.Add(i, new int[loops][]);
                for (int loop = 0; loop < loops; loop++)
                {
                    taskResults[i][loop] = new int[26];
                }
                WriteLine($"Main - starting task job {jobNumber}");
                Task.Run(() => CalculatorInTask(jobNumber, partitionSize, barrier, data, loops, taskResults[jobNumber]));
            }
            barrier.SignalAndWait();
            for (int loop = 0; loop < 5; loop++)
            {
                int[][] resultCollection1 = taskResults[0];
                int[][] resultCollection2 = taskResults[1];
                var resultCollection = resultCollection1[loop].Zip(resultCollection2[loop], (c1, c2) => c1 + c2);
                char ch = 'a';
                int sum = 0;
                foreach (var x in resultCollection)
                {
                    WriteLine($"{ch++}, cout: {x}");
                    sum += x;
                }
                LogBarrierInformation($"main task finished loop {loop}, sum: {sum}", barrier);
                WriteLine("finished all iterations");
            }
            barrier.RemoveParticipants(3);
            LogBarrierInformation("Remove bariers", barrier);
        }
        #region Barrier类
        static IEnumerable<string> FillData(int size)
        {
            var random = new Random();
            return Enumerable.Range(0, size).Select(x => GetString(random));
        }
        static string GetString(Random random)
        {
            var strBuilder = new StringBuilder(6);
            for (int i = 0; i < 6; i++)
            {
                strBuilder.Append((char)(random.Next(26)+97));
            }
            return strBuilder.ToString();
        }
        static void LogBarrierInformation(string info,Barrier barrier)
        {
            WriteLine($"Task {Task.CurrentId}: {info}." +
                $"{barrier.ParticipantCount} current and " +
                $"{barrier.ParticipantsRemaining} remaining participants," +
                $"phase {barrier.CurrentPhaseNumber}");
        }
        static void CalculatorInTask(int jobNumber,int partitionSize,Barrier barrier,
            IList<string>[] stringArray,int loops,int[][] results)
        {
            LogBarrierInformation("CalculatorInTask started",barrier);
            for (int i = 0; i < 5; i++)
            {
                var data = new List<string>(stringArray[i]);
                int start = jobNumber * partitionSize;
                int end = start + partitionSize;
                WriteLine($"Task {Task.CurrentId} in loop {i}: partition from {start} to {end}");
                for (int j = start; j < end; j++)
                {
                    var str = data[j];
                    char c = str[0];
                    var result = results[i]; //results是一个results[5][]锯齿数组, results[i]是一个int[26]数组
                    result[c - 97]++; //如果c = 'a',result[97-97]++ = result[0]++,即代表a出现了一次,int[0]的值加1
                }
                WriteLine($"Calculation completed from task {Task.CurrentId} ," +
                    $"int loop {i}. {results[i][0]} tiems a,{results[i][25]} times z");
            }
            LogBarrierInformation("sending signal and wait for all", barrier);
            barrier.SignalAndWait();
            LogBarrierInformation("waiting completed", barrier);
        }
        #endregion
    }
}

输出结果:

Task : initial participants in barrier.3 current and 3 remaining participants,phase 0
Main - starting task job 0
Main - starting task job 1
Task 1: CalculatorInTask started.3 current and 2 remaining participants,phase 0
Task 2: CalculatorInTask started.3 current and 2 remaining participants,phase 0
Task 2 in loop 0: partition from 1000000 to 2000000
Task 1 in loop 0: partition from 0 to 1000000
Calculation completed from task 1 ,int loop 0. 38413 tiems a,38408 times z
Calculation completed from task 2 ,int loop 0. 38300 tiems a,38474 times z
Task 1 in loop 1: partition from 0 to 1000000
Task 2 in loop 1: partition from 1000000 to 2000000
Calculation completed from task 2 ,int loop 1. 38395 tiems a,38357 times z
Calculation completed from task 1 ,int loop 1. 38285 tiems a,38416 times z
Task 1 in loop 2: partition from 0 to 1000000
Task 2 in loop 2: partition from 1000000 to 2000000
Calculation completed from task 2 ,int loop 2. 38242 tiems a,38245 times z
Calculation completed from task 1 ,int loop 2. 38446 tiems a,38378 times z
Task 2 in loop 3: partition from 1000000 to 2000000
Task 1 in loop 3: partition from 0 to 1000000
Calculation completed from task 2 ,int loop 3. 38265 tiems a,38779 times z
Calculation completed from task 1 ,int loop 3. 38589 tiems a,38524 times z
Task 2 in loop 4: partition from 1000000 to 2000000
Task 1 in loop 4: partition from 0 to 1000000
Calculation completed from task 2 ,int loop 4. 38743 tiems a,38080 times z
Calculation completed from task 1 ,int loop 4. 38643 tiems a,38461 times z
Task 1: sending signal and wait for all.3 current and 2 remaining participants,phase 0
Task 2: sending signal and wait for all.3 current and 2 remaining participants,phase 0
Task 2: waiting completed.3 current and 3 remaining participants,phase 1
Task 1: waiting completed.3 current and 3 remaining participants,phase 1
a, cout: 76713
b, cout: 76933
c, cout: 77061
d, cout: 76884
e, cout: 77211
f, cout: 76974
g, cout: 76959
h, cout: 76586
i, cout: 76376
j, cout: 77031
k, cout: 76792
l, cout: 76708
m, cout: 77111
n, cout: 77158
o, cout: 76859
p, cout: 77050
q, cout: 76732
r, cout: 77002
s, cout: 77043
t, cout: 76850
u, cout: 76886
v, cout: 76943
w, cout: 76702
x, cout: 77660
y, cout: 76894
z, cout: 76882
Task : main task finished loop 0, sum: 2000000.3 current and 3 remaining participants,phase 1
finished all iterations
a, cout: 76680
b, cout: 76621
c, cout: 76746
d, cout: 77224
e, cout: 77059
f, cout: 77433
g, cout: 77169
h, cout: 76847
i, cout: 76954
j, cout: 77125
k, cout: 77180
l, cout: 76663
m, cout: 77069
n, cout: 77230
o, cout: 76818
p, cout: 76917
q, cout: 77003
r, cout: 76903
s, cout: 76613
t, cout: 76214
u, cout: 77343
v, cout: 76749
w, cout: 76709
x, cout: 77157
y, cout: 76801
z, cout: 76773
Task : main task finished loop 1, sum: 2000000.3 current and 3 remaining participants,phase 1
finished all iterations
a, cout: 76688
b, cout: 77293
c, cout: 77181
d, cout: 76865
e, cout: 77007
f, cout: 76834
g, cout: 76804
h, cout: 77001
i, cout: 76772
j, cout: 77249
k, cout: 76307
l, cout: 77032
m, cout: 77145
n, cout: 76885
o, cout: 76807
p, cout: 77154
q, cout: 76755
r, cout: 76925
s, cout: 76656
t, cout: 76859
u, cout: 77532
v, cout: 76847
w, cout: 77052
x, cout: 76750
y, cout: 76977
z, cout: 76623
Task : main task finished loop 2, sum: 2000000.3 current and 3 remaining participants,phase 1
finished all iterations
a, cout: 76854
b, cout: 76667
c, cout: 76490
d, cout: 76848
e, cout: 77220
f, cout: 76918
g, cout: 76861
h, cout: 76950
i, cout: 77118
j, cout: 77023
k, cout: 76436
l, cout: 76867
m, cout: 76834
n, cout: 77025
o, cout: 77603
p, cout: 76552
q, cout: 76897
r, cout: 76981
s, cout: 77417
t, cout: 76660
u, cout: 76728
v, cout: 76979
w, cout: 76537
x, cout: 77163
y, cout: 77069
z, cout: 77303
Task : main task finished loop 3, sum: 2000000.3 current and 3 remaining participants,phase 1
finished all iterations
a, cout: 77386
b, cout: 76976
c, cout: 76851
d, cout: 77199
e, cout: 77128
f, cout: 77141
g, cout: 77201
h, cout: 77093
i, cout: 76659
j, cout: 76416
k, cout: 76898
l, cout: 77425
m, cout: 77500
n, cout: 76542
o, cout: 76759
p, cout: 76395
q, cout: 76911
r, cout: 77116
s, cout: 76851
t, cout: 76544
u, cout: 76998
v, cout: 76760
w, cout: 76951
x, cout: 76744
y, cout: 77015
z, cout: 76541
Task : main task finished loop 4, sum: 2000000.3 current and 3 remaining participants,phase 1
finished all iterations
Task : Remove bariers.0 current and 0 remaining participants,phase 1

 

展开阅读全文
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术工厂 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读