并行学习小结,直接上源代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Threading.Tasks;
using
System.Threading;
using
System.Diagnostics;
using
System.Collections.Concurrent;
public
partial
class
test : System.Web.UI.Page
{ protected
void
Page_Load( object
sender, EventArgs e)
{
UseParallel();
UsePLINQ();
}
/// <summary>
/// PLINQ的基本方法应用集合
/// </summary>
private
void
UsePLINQ()
{
#region 并行集合运算,Environment.ProcessorCount - 1 给一个处理器出来处理其他任务
ConcurrentDictionary< int , string > cds = new
ConcurrentDictionary< int , string >(); //并行考虑用的集合
Parallel.For(0, 3000, i =>
{
cds.TryAdd(i, Math.Sqrt(i).ToString());
});
var
q1 = from
c in
cds.Values.AsParallel().WithDegreeOfParallelism(Environment.ProcessorCount - 1)
select
c; //直接获取该集合,Environment.ProcessorCount - 1 给一个处理器出来处理其他任务
#endregion
#region 并行常用的运算
ConcurrentBag< int > bags = new
ConcurrentBag< int >(); //线程安全的集合
var
list = ParallelEnumerable.Range(0,3000); //生成并行集合
list.ForAll(i => {
bags.Add(i);
});
Response.Write( "bags长度:"
+ bags.Count+ "<br/>" );
Response.Write( "list整数和:"
+ list.Sum() + "<br/>" );
Response.Write( "list最大值:"
+ list.Max() + "<br/>" );
Response.Write( "list最小值:"
+ list.Min() + "<br/>" );
Response.Write( "list平均值:"
+ list.Average() + "<br/>" );
Response.Write( "list默认值:"
+ list.FirstOrDefault() + "<br/>" );
#endregion
#region 先并行分组,在对每组并行查询统计
List<Student> list2 = new
List<Student>()
{
new
Student()
{
ID = 1,
Name = "jack" ,
Age = 20
},
new
Student()
{
ID = 1,
Name = "mary" ,
Age = 25
},
new
Student()
{
ID = 1,
Name = "joe" ,
Age = 29
},
new
Student()
{
ID = 1,
Name = "Aaron" ,
Age = 25
}
};
var
map = list2.AsParallel().ToLookup(i => i.Age,count=>1); //化简统计
var
reduce = from
IGrouping< int , int > singleMap
in
map.AsParallel()
select
new {
Age = singleMap.Key,
Count = singleMap.Count()
};
reduce.ForAll(i =>{
Response.Write( string .Format( "当前Age={0}的人数有:{1}人<br/>" , i.Age, i.Count));
});
#endregion
foreach
( var
o in
q1)
{
Response.Write(o + "<br/>" );
}
}
/// <summary>
/// Parallel的基本使用方法集合
/// </summary>
private
void
UseParallel()
{
#region 并行查询和并行输出
List< string > lists = new
List< string >();
for
( int
i = 0; i < 1000; i++)
{
lists.Add(i.ToString());
}
var
q1 = from
p in
lists.AsParallel()
where
Convert.ToInt32(p) > 100
select
p; // 并行查询
q1.ForAll< string >(p =>
{
Response.Write(p + "<br/>" ); //并行输出,注意观察输出结果不是有序的
});
#endregion
#region 串行执行任务
Stopwatch sw = new
Stopwatch();
sw.Start();
Run1();
Run2();
Response.Write( "串行执行时间:"
+ sw.ElapsedMilliseconds + "<br/>" );
#endregion
#region 并行执行任务
sw.Restart();
Parallel.Invoke(Run1, Run2);
Response.Write( "并行执行时间:"
+ sw.ElapsedMilliseconds + "<br/>" );
#endregion
#region 并行赋值
List< int > mylist = new
List< int >();
Parallel.ForEach(Partitioner.Create(0, 5000), i =>
{
for
( int
m = i.Item1; m < i.Item2; m++)
{
mylist.Add(m);
}
});
#endregion
#region 并行 退出循环 Break和Stop
List< int > mylist2 = new
List< int >();
Parallel.For(0, 5000, (i, state) =>
{
if
(mylist2.Count == 50)
{
state.Break();
return ;
}
mylist2.Add(i);
});
#endregion
#region 并行抛出异常处理
try
{
Parallel.Invoke(Run1, Run2, Run3);
}
catch
(AggregateException ex)
{
foreach
( var
single in
ex.InnerExceptions)
{
Response.Write(single.Message + "<br/>" );
}
}
#endregion
}
private
void
Run1()
{
Thread.Sleep(3000);
}
private
void
Run2()
{
Thread.Sleep(5000);
}
private
void
Run3()
{
Thread.Sleep(1000);
}
private
void
Run4( int
ms)
{
Thread.Sleep(ms * 100);
}
} public
class
Student {
public
int
ID { get ; set ; }
public
string
Name { get ; set ; }
public
int
Age { get ; set ; }
public
DateTime CreateTime { get ; set ; }
} |