2024年2月20日发(作者:)
计算机交流平台:计算机故障_计算机论坛 wy
C#并发集合的简单方法
微软对C#(4.0)的框架添加了全新的并发编程框架,现在我们也能用C#开发支持并发概念的程序的。在并发编程中最让人烦恼的应该就是如何数据同步:避免脏读和脏写,当然我们可以通过Lock技术来实现,也可以使用微软提供给我们的并发集合,这些集合都提供了TryDo方法。用它们对数据的读/写操作能在TryDo返回True的情况下执行。我们来看看它们吧:
IProducerConsumerCollection
所有的并发集合都实现了这个接口,TryAdd和TryTake分别在读和写的时候判断是否能正常进行,不行则返回false。
[csharp]
public interface IProducerConsumerCollection
{
void CopyTo(T[] array, int index);
T[] ToArray();
bool TryAdd(T item);
bool TryTake(out T item);
}
public interface IProducerConsumerCollection
{
void CopyTo(T[] array, int index);
T[] ToArray();
bool TryAdd(T item);
bool TryTake(out T item);
}
ConcurrentQueue
并发队列,队列类型的数据结构。
[csharp]
public static void ConcurrentQueueTest()
{
() =>
{
while ( != 26)
{
if ( == 0)
{
for (int i = 65; i <= 90; i++)
{
e((i).ToString());
}
}
}
ine("-------------start------------");
计算机交流平台:计算机故障_计算机论坛 wy
return strNornalQueue;
},
(index, state, head) =>
{
string tmp = ;
tmp = e();
ine(("The element '{0}' was set by thread {1}", tmp,
dThreadId));
e(tmp);
return strNornalQueue;
},
(result) =>
{
ine("-------------end------------------");
});
ine(("current collection has {0} elements, Has duplicate data:{1}",
, IsDuplicate
foreach (string item in strNornalQueue)
{
ine(item);
}
}
public static void ConcurrentQueueTest()
{
() =>
{
while ( != 26)
{
if ( == 0)
{
for (int i = 65; i <= 90; i++)
{
e((i).ToString());
}
}
}
ine("-------------start------------");
return strNornalQueue;
},
(index, state, head) =>
计算机交流平台:计算机故障_计算机论坛 wy
{
string tmp = ;
tmp = e();
ine(("The element '{0}' was set by thread {1}", tmp,
dThreadId));
e(tmp);
return strNornalQueue;
},
(result) =>
{
ine("-------------end------------------");
});
ine(("current collection has {0} elements, Has duplicate data:{1}",
, IsDuplicate
foreach (string item in strNornalQueue)
{
ine(item);
}
}
ConcurrentStack
并发栈,栈类型的数据结构。
[csharp]
public static void ConcurrentStackTest()
{
() =>
{
while ( != 26)
{
if ( == 0)
{
for (int i = 65; i <= 90; i++)
{
((i).ToString());
}
}
}
ine("-------------start------------");
return strStack;
},
(index, state, head) =>
计算机交流平台:计算机故障_计算机论坛 wy
{
string tmp = ;
if ((out tmp))
{
ine(("The element '{0}' was set by thread {1}", tmp,
dThreadId));
(tmp);
}
else
{
ine("queue is buzy now");
}
return strStack;
},
(result) =>
{
ine("-------------end------------------");
});
ine(("current collection has {0} elements, Has duplicate data:{1}",
, IsDuplicate
foreach (string item in strStack)
{
ine(item);
}
}
public static void ConcurrentStackTest()
{
() =>
{
while ( != 26)
{
if ( == 0)
{
for (int i = 65; i <= 90; i++)
{
((i).ToString());
}
}
}
ine("-------------start------------");
return strStack;
},
计算机交流平台:计算机故障_计算机论坛 wy
(index, state, head) =>
{
string tmp = ;
if ((out tmp))
{
ine(("The element '{0}' was set by thread {1}", tmp,
dThreadId));
(tmp);
}
else
{
ine("queue is buzy now");
}
return strStack;
},
(result) =>
{
ine("-------------end------------------");
});
ine(("current collection has {0} elements, Has duplicate data:{1}",
, IsDuplicate
foreach (string item in strStack)
{
ine(item);
}
}
ConcurrentDictionary
并发字典,字典类型的数据结构。
[csharp]
public static void ConcurrentDictionary()
{
for (int i = 65; i <= 90; i++)
{
((i).ToString(), (i).ToString());
}
() =>
{
ine("-------------start------------");
return strDictionary;
},
计算机交流平台:计算机故障_计算机论坛 wy
(index, state, head) =>
{
string tmp = ;
if (ove((new Random().Next(65,90)).ToString(), out tmp))
{
ine(("The element '{0}' was set by thread {1}", tmp,
dThreadId));
(tmp, tmp);
}
else
{
ine("queue is buzy now");
}
return strDictionary;
},
(result) =>
{
ine("-------------end------------------");
});
}
public static void ConcurrentDictionary()
{
for (int i = 65; i <= 90; i++)
{
((i).ToString(), (i).ToString());
}
() =>
{
ine("-------------start------------");
return strDictionary;
},
(index, state, head) =>
{
string tmp = ;
if (ove((new Random().Next(65,90)).ToString(), out tmp))
{
ine(("The element '{0}' was set by thread {1}",
dThreadId));
(tmp, tmp);
}
tmp,
计算机交流平台:计算机故障_计算机论坛 wy
else
{
ine("queue is buzy now");
}
return strDictionary;
},
(result) =>
{
ine("-------------end------------------");
});
}
ConcurrentBag
类似堆栈的数据结构。
[csharp]
public static void ConcurrentBag()
{
for (int i = 65; i <= 90; i++)
{
((i).ToString());
}
() =>
{
return strBag;
},
(index, state, head) =>
{
string tmp = ;
if (e(out tmp))
{
ine(("The element '{0}' was
dThreadId));
(tmp);
}
else
{
ine("queue is buzy now");
}
return strBag;
},
set by thread tmp, {1}",
计算机交流平台:计算机故障_计算机论坛 wy
(result) =>
{
ine("-------------end------------------");
});
ine(("current collection has {0} elements, Has duplicate data:{1}",
, IsDuplicate
foreach (string item in strBag)
{
ine(item);
}
}
public static void ConcurrentBag()
{
for (int i = 65; i <= 90; i++)
{
((i).ToString());
}
() =>
{
return strBag;
},
(index, state, head) =>
{
string tmp = ;
if (e(out tmp))
{
ine(("The element '{0}' was set by thread {1}", tmp,
dThreadId));
(tmp);
}
else
{
ine("queue is buzy now");
}
return strBag;
},
(result) =>
{
ine("-------------end------------------");
});
ine(("current collection has {0} elements, Has duplicate data:{1}",
计算机交流平台:计算机故障_计算机论坛 wy
, IsDuplicate
foreach (string item in strBag)
{
ine(item);
}
}
BlockingCollection
并发集合,在程序操作完之前会一直阻塞其他程序对其进行操作。
[csharp]
public static void BlockingCollectionTest()
{
() =>
{
while ( != 26)
{
if ( == 0)
{
for (int i = 65; i <= 90; i++)
{
((i).ToString());
}
}
}
ine("-------------start------------");
return strBlockCollection;
},
(index, state, head) =>
{
string tmp = ;
tmp=();
ine(("The element '{0}' was
dThreadId));
(tmp);
return strBlockCollection;
},
(result) =>
{
ine("-------------end------------------");
});
set by thread {1}", tmp,
计算机交流平台:计算机故障_计算机论坛 wy
ine(("current collection has {0} elements, Has duplicate data:{1}",
,
IsDuplicate
foreach (string item in strBlockCollection)
{
ine(item);
}
}
public static void BlockingCollectionTest()
{
() =>
{
while ( != 26)
{
if ( == 0)
{
for (int i = 65; i <= 90; i++)
{
((i).ToString());
}
}
}
ine("-------------start------------");
return strBlockCollection;
},
(index, state, head) =>
{
string tmp = ;
tmp=();
ine(("The element '{0}' was set by thread {1}", tmp,
dThreadId));
(tmp);
return strBlockCollection;
},
(result) =>
{
ine("-------------end------------------");
});
ine(("current collection has {0} elements, Has duplicate data:{1}",
,
计算机交流平台:计算机故障_计算机论坛 wy
IsDuplicate
foreach (string item in strBlockCollection)
{
ine(item);
}
}


发布评论