多线程访问集合 [英] Multiple Thread Access a Collection
问题描述
假设有多个线程访问的集合。一个线程需要知道一个给定的项目已经被另一个线程添加。任何人都可以给出解决方案,使它吗?感谢。
Supposed there is collection which is accessed by multiple thread. One of thread needs to know a given item had been added by another thread. Can anyone give a solution to make it ? Thanks.
推荐答案
从线程安全集合开始
至于知道什么线程添加了特定项目我不认为一个项目跟踪。我想你可以添加一个属性TheadName,并让线程输入该值。
As for knowing what thread added specific item I don't think an item tracks the tread. I think you could add a Property TheadName and have the thread enter that value.
问题陈述特定项目已添加。然后在注释中,OP推断出任何项目。
The question states "specific item had been added". Then in comment the OP infers any item.
.NET没有线程安全的ObservableCollection。有一些关于codeplex或可能重新思考你设计。 ConcurrentQueue可能是一个选项。
.NET does not have thread safe ObservableCollection. There are some on codeplex or possible rethink you design. ConcurrentQueue may be an option.
这篇关于多线程访问集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!