C# 线程安全的集合
public void Test()
{
///线程安全的先进先出集合
ConcurrentQueue<string> queue1 = new ConcurrentQueue<string>();
queue1.Enqueue("测试");//添加元素
queue1.TryDequeue(out string first1);//尝试移除并返回开头对象
///线程安全的后进先出集合
ConcurrentStack<string> queue2 = new ConcurrentStack<string>();
queue2.Push("测试");//添加元素
queue2.TryPop(out string first2);//尝试移除并返回开头对象
///线程安全的无序集合
ConcurrentBag<string> queue3 = new ConcurrentBag<string>();
queue3.Add("测试");//添加元素
queue3.TryTake(out string first3);//尝试移除并返回开头对象
ConcurrentDictionary<string, string> queue4 = new ConcurrentDictionary<string, string>();
queue4.TryAdd("1", "测试");
queue4.TryUpdate("1", "测试1", "测试");
queue4.TryRemove("1", out string value);
}
Lingq多线程执行
IEnumerable<IntervalMinMax> intervals;
intervals = columns
.AsParallel()
.AsOrdered()
.Select(xPx => CalcInterval(xPx, offsetPoints, columnPointCount, dims))
.AsSequential();
多线程循环
Parallel.For(0, height, i =>
{
for (int j = 0; j < width; j++)
{
destination[i, j] = source[i, j];
}
});
发表回复