何时使用BlockingCollection当ConcurrentBag而不是List< T&GT ;? [英] When to use BlockingCollection and when ConcurrentBag instead of List<T>?

查看:707
本文介绍了何时使用BlockingCollection当ConcurrentBag而不是List< T&GT ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

接受的答案质疑为什么这个Parallel.ForEach代码冻结计划了吗? 建议来替代在WPF应用程序列表的使用由 ConcurrentBag

The accepted answer to question "Why does this Parallel.ForEach code freeze the program up?" advises to substitute the List usage by ConcurrentBag in a WPF application.

我想知道是否的 BlockingCollection 可以在这种情况下,代替使用?

I'd like to understand whether a BlockingCollection can be used in this case instead?

推荐答案

您确实可以使用 BlockingCollection ,但也绝对没有点这样做。

You can indeed use a BlockingCollection, but there is absolutely no point in doing so.

首先,注意的 BlockingCollection 是围绕一个集合的包装实现的 IProducerConsumerCollection< T> 。实现该接口的任何类型都可以用作底层存储:

First off, note that BlockingCollection is a wrapper around a collection that implements IProducerConsumerCollection<T>. Any type that implements that interface can be used as the underlying storage:

当你创建一个 BlockingCollection< T> 对象时,可以b $未指定$ b只有有限能力,而且集合类型使用。对于
例如,您可以指定 ConcurrentQueue< T>作为第一,
先出(FIFO)的行为,或<$ C $
对象C> ConcurrentStack< T>作为去年
对象,先出(LIFO)的行为。你可以使用任何集合类
实现了 IProducerConsumerCollection< T> 接口。默认
集合类型为 BlockingCollection< T> ConcurrentQueue< T>

When you create a BlockingCollection<T> object, you can specify not only the bounded capacity but also the type of collection to use. For example, you could specify a ConcurrentQueue<T> object for first in, first out (FIFO) behavior, or a ConcurrentStack<T> object for last in,first out (LIFO) behavior. You can use any collection class that implements the IProducerConsumerCollection<T> interface. The default collection type for BlockingCollection<T> is ConcurrentQueue<T>.

这包括 ConcurrentBag< T> ,这意味着你可以有阻塞并发袋。那么,什么是一个普通的区别 IProducerConsumerCollection< T> 和阻挡收藏?的 BlockingCollection 文档说(重点煤矿):

This includes ConcurrentBag<T>, which means you can have a blocking concurrent bag. So what's the difference between a plain IProducerConsumerCollection<T> and a blocking collection? The documentation of BlockingCollection says (emphasis mine):

BlockingCollection< T> 被用作一个
的包装 IProducerConsumerCollection< T> 例如,允许拆除尝试$ b从集合$ b键阻塞,直到数据可被删除即可。
同样, BlockingCollection< T> 可以创建为强制执行
上限允许的数据元素的数量

IProducerConsumerCollection< T> [...]

BlockingCollection<T> is used as a wrapper for an IProducerConsumerCollection<T> instance, allowing removal attempts from the collection to block until data is available to be removed. Similarly, a BlockingCollection<T> can be created to enforce an upper-bound on the number of data elements allowed in the IProducerConsumerCollection<T> [...]

由于链接的问题,没有必要做任何的这些东西,使用 BlockingCollection 简单地增加功能,最后都用不上了一层。

Since in the linked question there is no need to do either of these things, using BlockingCollection simply adds a layer of functionality that goes unused.

这篇关于何时使用BlockingCollection当ConcurrentBag而不是List&LT; T&GT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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