回答編集履歴
2
追記
answer
CHANGED
@@ -1,3 +1,88 @@
|
|
1
1
|
Parallel.ForEach, Parallel.For メソッドのオーバロードの中には引数に ParallelOptions を取るものがあるので、その MaxDegreeOfParallelism プロパティを使って望むこと (上限のある並列数で実行) はできませんか?
|
2
2
|
|
3
|
-
(UI への応答性を高めるためのバックグラウンド処理をしたいのか、マルチコアを利用して処理時間の短縮を図りたいのか目的が分かりません。なんとなく見当違いのことのように思いますが、それはとりあえず置いときます)
|
3
|
+
(UI への応答性を高めるためのバックグラウンド処理をしたいのか、マルチコアを利用して処理時間の短縮を図りたいのか目的が分かりません。なんとなく見当違いのことのように思いますが、それはとりあえず置いときます)
|
4
|
+
|
5
|
+
**【追記】**
|
6
|
+
|
7
|
+
上に書いたことを検証してみました。
|
8
|
+
|
9
|
+
> listのデータに対して順序にかかわらず上限のある並列数(1~)で実行したい。システム全体でのスレッドの上限があるため、極力節約したい。
|
10
|
+
|
11
|
+
その目的のために await を使う意味が分かりません。ParallelOptions.MaxDegreeOfParallelism を設定して、普通に以下のコードのようにすれば上記の目的は果たせるはずですが?
|
12
|
+
|
13
|
+
```
|
14
|
+
var list = Enumerable.Range(1, 30).ToList();
|
15
|
+
ParallelOptions option = new ParallelOptions();
|
16
|
+
option.MaxDegreeOfParallelism = 2;
|
17
|
+
Parallel.ForEach(list, option, i =>
|
18
|
+
{
|
19
|
+
Console.WriteLine($"Processing {i} on thread {Thread.CurrentThread.ManagedThreadId}");
|
20
|
+
});
|
21
|
+
```
|
22
|
+
|
23
|
+
上記のコード(MaxDegreeOfParallelism は 2 に制限)の実行結果は以下の通りとなります。質問者さんのコードでは 10 に制限しているように見えますが、スレッドの数はそのようになりませんか?
|
24
|
+
|
25
|
+
Processing 1 on thread 1
|
26
|
+
Processing 16 on thread 3
|
27
|
+
Processing 17 on thread 3
|
28
|
+
Processing 18 on thread 3
|
29
|
+
Processing 19 on thread 3
|
30
|
+
Processing 20 on thread 3
|
31
|
+
Processing 21 on thread 3
|
32
|
+
Processing 22 on thread 3
|
33
|
+
Processing 23 on thread 3
|
34
|
+
Processing 24 on thread 3
|
35
|
+
Processing 25 on thread 3
|
36
|
+
Processing 26 on thread 3
|
37
|
+
Processing 27 on thread 3
|
38
|
+
Processing 28 on thread 3
|
39
|
+
Processing 29 on thread 3
|
40
|
+
Processing 30 on thread 3
|
41
|
+
Processing 4 on thread 3
|
42
|
+
Processing 5 on thread 3
|
43
|
+
Processing 6 on thread 3
|
44
|
+
Processing 7 on thread 3
|
45
|
+
Processing 8 on thread 3
|
46
|
+
Processing 9 on thread 3
|
47
|
+
Processing 10 on thread 3
|
48
|
+
Processing 11 on thread 3
|
49
|
+
Processing 12 on thread 3
|
50
|
+
Processing 13 on thread 3
|
51
|
+
Processing 14 on thread 3
|
52
|
+
Processing 2 on thread 1
|
53
|
+
Processing 3 on thread 1
|
54
|
+
Processing 15 on thread 3
|
55
|
+
|
56
|
+
|
57
|
+
ちなみに、ParallelOptions を設定しない場合は以下のようになります。
|
58
|
+
|
59
|
+
Processing 1 on thread 1
|
60
|
+
Processing 2 on thread 1
|
61
|
+
Processing 16 on thread 7
|
62
|
+
Processing 7 on thread 4
|
63
|
+
Processing 8 on thread 4
|
64
|
+
Processing 9 on thread 4
|
65
|
+
Processing 13 on thread 6
|
66
|
+
Processing 14 on thread 6
|
67
|
+
Processing 15 on thread 6
|
68
|
+
Processing 19 on thread 9
|
69
|
+
Processing 4 on thread 3
|
70
|
+
Processing 10 on thread 5
|
71
|
+
Processing 26 on thread 5
|
72
|
+
Processing 27 on thread 5
|
73
|
+
Processing 28 on thread 5
|
74
|
+
Processing 29 on thread 5
|
75
|
+
Processing 30 on thread 5
|
76
|
+
Processing 22 on thread 8
|
77
|
+
Processing 11 on thread 4
|
78
|
+
Processing 20 on thread 6
|
79
|
+
Processing 25 on thread 10
|
80
|
+
Processing 3 on thread 1
|
81
|
+
Processing 17 on thread 7
|
82
|
+
Processing 18 on thread 7
|
83
|
+
Processing 21 on thread 6
|
84
|
+
Processing 12 on thread 4
|
85
|
+
Processing 5 on thread 3
|
86
|
+
Processing 6 on thread 3
|
87
|
+
Processing 23 on thread 9
|
88
|
+
Processing 24 on thread 9
|
1
追記
answer
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
Parallel.ForEach, Parallel.For メソッドのオーバロードの中には引数に ParallelOptions を取るものがあるので、その MaxDegreeOfParallelism プロパティを使って望むことはできませんか?
|
1
|
+
Parallel.ForEach, Parallel.For メソッドのオーバロードの中には引数に ParallelOptions を取るものがあるので、その MaxDegreeOfParallelism プロパティを使って望むこと (上限のある並列数で実行) はできませんか?
|
2
2
|
|
3
3
|
(UI への応答性を高めるためのバックグラウンド処理をしたいのか、マルチコアを利用して処理時間の短縮を図りたいのか目的が分かりません。なんとなく見当違いのことのように思いますが、それはとりあえず置いときます)
|