csharparalle_problem

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
string[] strs= new string[100];
for (int i = 0; i < 100; i++)
{
strs[i] = DateTime.Now.Add(TimeSpan.FromDays(i)).ToString("yyyy-MM-dd");
}

string[] strs2 = new string[100];
for (int i = 0; i < 100; i++)
{
strs2[i] = DateTime.Now.AddYears(1).Add(TimeSpan.FromDays(i)).ToString("yyyy-MM-dd");
}

string[] strs1 = new string[100];
for (int i = 0; i < 100; i++)
{
strs1[i] = DateTime.Now.Add(TimeSpan.FromDays(i)).ToString("HH:mm:ss");
}

ConcurrentDictionary<int, string> dic = new ConcurrentDictionary<int, string>();

ConcurrentDictionary<DateTime,List<int>> dic1 = new ConcurrentDictionary<DateTime,List<int>>();

ParallelLoopResult result = Parallel.For(0, 10, (i) =>
{
dic1.AddOrUpdate(DateTime.Now.Date,(k) =>
{
// Thread.Sleep(1000);
List<int> newList = new List<int>();
newList.Add(i);
foreach (var item in newList)
{
Console.WriteLine("init add :" + item + " " + DateTime.Now.ToString("HH:mm:ss fff"));
}
return newList;
}, (k, v) =>
{
//Console.WriteLine("Task:update index:" + i + ":" + v + " to " + strs[i]);
// Thread.Sleep(1000);
v.Add(i);
Console.WriteLine("init update : " + DateTime.Now.ToString("HH:mm:ss fff"));
return v;
});

List<int> countList = null;
if (dic1.TryRemove(DateTime.Now.Date, out countList) && countList.Count >= 2)
{
//这边会报集合已修改,无法进行遍历,看来countList线程共享?

//因为上面有可能会有其他线程的update操作
foreach (var item in countList)
{
Console.WriteLine("removed :" + " " + DateTime.Now.ToString("HH:mm:ss fff"));
}
}
else if (countList != null)
{
dic1.AddOrUpdate(DateTime.Now.Date, (k) =>

{
// Thread.Sleep(1000);
foreach (var item in countList)
{
Console.WriteLine("also add : "+ DateTime.Now.ToString("HH:mm:ss fff"));
}
return countList;
}, (k, v) =>
{
//Console.WriteLine("Task:update index:" + i + ":" + v + " to " + strs[i]);
// Thread.Sleep(1000);
v.AddRange(countList);

foreach (var item in v)
{
Console.WriteLine("update : " + DateTime.Now.ToString("HH:mm:ss fff"));
}


return v;
});
}


});

//while (!result.IsCompleted)
//{

//}

//foreach (var item in dic1)
//{
// List<int> intList = item.Value;
// for (int i = 0; i < intList.Count; i++)
// {
// Console.WriteLine(intList[i]);
// }

//}
Console.Read();


Task task0 = Task.Run(() =>

{
for (int i = 0; i < 100; i++)
{
dic.AddOrUpdate(i, (k) =>{
// Thread.Sleep(1000);
Console.WriteLine("Task:add:" +" index:"+i+" "+ strs[i]);
return strs[i];

}, (k, v) =>
{
Console.WriteLine("Task:update index:"+i+":"+ v + " to " + strs[i]);
// Thread.Sleep(1000);
return strs[i];
});
}
});

//task0.Start();

Task task1 = Task.Run(() =>
{
for (int i = 0; i < 100; i++)
{
string outValue ;
dic.TryRemove(i, out outValue);
if (!string.IsNullOrEmpty(outValue))
{
Console.WriteLine("Task1:remove:" + " index:" + i + " " + outValue);
dic.AddOrUpdate(i, (k) =>
{
// Thread.Sleep(1000);
Console.WriteLine("Task1:add:" + " index:" + i + " " + strs1[i]);
return strs1[i];

}, (k, v) =>
{
Console.WriteLine("Task1:update index:" + i + ":" + v + " to " + strs1[i]);
//Thread.Sleep(1000);
return strs1[i];
});
}
else
{
Console.WriteLine("Task1:remove:" + " index:" + i + " empty");
}


}
});

Task task2 = Task.Run(() =>
{
for (int i = 0; i < 100; i++)
{
dic.AddOrUpdate(i, (k) =>
{
// Thread.Sleep(1000);
Console.WriteLine("Task2:add:" + " index:" + i + " " + strs2[i]);
return strs2[i];

}, (k, v) =>
{
Console.WriteLine("Task2:update index:" + i + ":" + v + " to " + strs2[i]);
// Thread.Sleep(1000);
return strs2[i];
});
}
});

Task.WaitAll();
//https://msdn.microsoft.com/zh-cn/library/dd997369.aspx
// task1.Start();
Console.Read();
欢迎关注我的公众号:沉迷Spring
显示 Gitment 评论
0%