Threading.Task.Parallel.For中的C#错误?
|
这是Parallel.For中的错误吗?
public class DataPoint
{
public int Game { get; set; }
public byte Card { get; set; }
public byte Location { get; set; }
public DataPoint(int g,byte c,byte Loc)
{
Game = g;
Card = c;
Location = Loc;
}
public override string ToString()
{
return String.Format(\"{0} {1} {2}\",Game,Card,Location);
}
}
它一定是Parallel.For中的错误
private static System.Collections.Concurrent.ConcurrentBag<DataPoint> FillData()
{
var points = new System.Collections.Concurrent.ConcurrentBag<DataPoint>();
long c = 32768;
long z = 0;
Parallel.For(z, c, (i) =>
{
points.Add(new DataPoint( rand.Next(1, 100001),
(byte)rand.Next(1, 144),
(byte)rand.Next(1, 40)));
});
return points;
}
作品
private static System.Collections.Concurrent.ConcurrentBag<DataPoint> FillData()
{
var points = new System.Collections.Concurrent.ConcurrentBag<DataPoint>();
long c = 32769;
long z = 0;
Parallel.For(z, c, (i) =>
{
points.Add(new DataPoint( rand.Next(1, 100001),
(byte)rand.Next(1, 144),
(byte)rand.Next(1, 40)));
});
return points;
}
并非各自默认为{1,1,1}
没有找到相关结果
已邀请:
3 个回复
呢率篓舍烫
和
不能很好地结合在一起。只需将Parallel.For替换为普通的
循环即可。对于32k元素,您不会注意到差异。 如果仍然需要并行处理,则必须拆分范围,运行几个Task,然后为每个Task分配自己的Random实例。
蕉衫
翱抹村