迭代通用并发集合 [英] Iterating over generic concurrent collections

查看:93
本文介绍了迭代通用并发集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以安全地迭代工作线程上的通用并发集合而不锁定它们(ConcurrentQueue,ConcurrentDictionary,ConcurrentStack等)?

解决方案

当然可以。从这些类的文档来看,即使从他们的名字中也不是那么明显吗?这就是他们的设计目标。此外,这样的集合可以作为线程的某种线程同步实用程序服务器,因为,例如,它们序列化由不同线程添加到同一集合的数据。



< DD> -SA

Is safe to iterate over a generic concurrent collections on worker threads without locking on them (ConcurrentQueue, ConcurrentDictionary, ConcurrentStack, etc)?

解决方案

Yes of course. Isn't that apparent from the documentation of these classes, even from their names? This is what they have been designed for. Moreover, such collections can server as a certain kind of thread synchronization utility for threads, because, say, they serialize data added to the same collection by different threads.

—SA


这篇关于迭代通用并发集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆