- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我基本上是在寻找一个线程中从相机获取的图像集合的容器。由于 ConcurrentQueue 是线程安全的,所以我想使用它。但是在调试我的代码时,我发现了 this article说
If the elements are small, you’ll probably never notice this. If, however, the elements hold on to large resources (e.g. each element is a huge image bitmap), it’s possible you could see the impact of this (one workaround is to queue a wrapper object, e.g. have a
ConcurrentQueue<StrongBox<T>>
rather than aConcurrentQueue<T>
, and null out the wrapper’s reference to the T value after the wrapper has been dequeued).
据我所知,StrongBox
是一种原始值的包装器。这是否意味着我必须存储另一个图像集合?
所以我正在寻找 ConcurrentQueue<StrongBox<T>>.
的用法或示例我从谷歌上找到的唯一东西是 this code.
最佳答案
过早优化的危险提醒在评论中,所以我将解决这里发生的事情的语义。
正如文章所指出的,ConcurrentQueue
可以坚持一些已经经历过的事情的引用。我把它学成“几十”,文章说它不超过 31,这似乎很好地融合了。如果队列跟踪大对象,如 2000x2000 位图,理论上会成为问题。当然,这取决于程序的其余部分在做什么。
将其包装在 StrongBox<T>
中有帮助,因为唯一的事情 StrongBox
所做的是坚持对其他事物的引用。因此,StrongBox
占地面积非常小,无论它持有什么都会超出范围并且(理论上)更快地获得 GC。
自 StrongBox
含有无糖汽水的所有成分,你有点想多了。您实际上只是加载了 Value
一些字段 T
稍后再引用。它看起来有点像这样:
var boxedBitmap = new StrongBox<Bitmap>(new Bitmap(1,1));
var bitmap = boxedBitmap.Value;
或者:
var boxedBitmap = new StrongBox<Bitmap>();
boxedBitmap.Value = new Bitmap(1,1);
var bitmap = boxedBitmap.Value;
说真的,如果你在 Reflector 中打开它,这个类的实现就像 5 行。
在这种情况下,您对 ConcurrentQueue<T>
的用法与 ConcurrentQueue<StrongBox<T>>
的用法并没有什么不同.您只需添加 .Value
在将资源发送到其目标线程之前。这确实帮助我工作的一家公司通过简单地传递对确定性工具的引用而不是传递整个工具来减少大量多线程分析服务的内存印记,但你的里程可能会有所不同 - 我不清楚如果你传递一些东西来改变然后被其他东西使用,它会产生什么后果。
关于c# - ConcurrentQueue<StrongBox<T>> 的用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13612098/
我有一个类可以从 Web 服务获取对象。我对这些对象的使用与顺序无关,因此我并行发出 Web 请求并将结果添加到 ConcurrentQueue 中。 同时,在发出请求时,我们可以处理响应。 因此,我
我在 C# (4.0) 中有一个常规 Queue 对象,我正在使用访问此 Queue 的 BackgroundWorkers。 我使用的代码如下: do { while
我在 C# (4.0) 中有一个常规 Queue 对象,我正在使用访问此 Queue 的 BackgroundWorkers。 我使用的代码如下: do { while
我希望两个线程与一个队列一起工作。第一个线程应该每 2 秒调用一次,第二个线程 - 每 3 秒调用一次。两个线程应同时启动。访问队列的第一个元素时出现问题。两个线程都获取索引为 0 的元素。有时它会发
我有生产者/消费者的问题。目前我有一个简单的 Queue 被 lock 包围。 我正在尝试用更高效的东西替换它。 我的第一选择是使用 ConcurrentQueue,但我不知道如何让我的消费者等待下一
我正在查看 Roslyn September 2012 CTP使用 Reflector,我注意到 SlidingTextWindow 类具有以下内容: internal sealed class Sl
我在使用 ConcurrentQueue 时发生内存泄漏: requestObject request = xxx; Item obj= new Item (); obj.MessageReceive
我必须维护信息日志,这些日志可以同时从多个线程写入,但是当我需要它们时,我只使用一个线程将其出列,在出列集合之间需要大约 5 秒的时间。 以下是我编写的用于将其出队的代码。 if (timeNotRe
这是一种安全的迭代方式吗 ConcurrentQueue ? while (cq.GetEnumerator().MoveNext()) { IIndexTask task; if (
为什么不ConcurrentQueue有一个 capacity喜欢它的非并发表亲? 也没有提到默认容量。 与非并发版本相比,“缺失”容量是否会影响性能,在非并发版本中,实现者可以提供对队列典型大小的合
我有多个线程生成项目并将它们粘贴在一个共同的 ConcurrentQueue 中: private ConcurrentQueue queuedItems = new ConcurrentQueue(
我有一个 NetworkStream 用于从另一个程序获取数据。数据以 Byte[64] 的形式到达,然后我将其排队到 ConcurrentQueue,以便另一个线程可以出队以供稍后分析。队列被实例化
我想问一下c#中的queue如果 ConcurrentQueue 是安全线程,为什么这段代码的结果是 ~98 k?我有什么问题吗? class Program { static int sum
我有一个应用程序,它有一个 ConcurrentQueue 项目,每个项目都有一个 ID 属性和一个 ConcurrentQueue 任务,队列项目看起来像: class QueueItem {
我正在串行端口上快速接收数据,而在其他线程中我正在处理串行端口上接收到的数据包。因此,对于线程安全的读写操作,我使用并发队列。以下是我在串行端口上接收数据的代码,我存储在并发队列中。 System.C
这很奇怪,我有一个 Thread[]每个工作线程处理 ConcurrentQueue 中的项目直到队列为空,此时程序的其余部分继续。 这一直有效到大约 1500 个项目,此时所有线程都在 WaitSl
我们有一个 ConcurrentQueue 用于在 3 个线程之间共享数据。线程 A 不断地用数据填充队列。线程 B 旨在将此数据记录到文件中。线程 C 应该检索队列中最年轻的条目(或尽可能接近最年轻
在查看 IsEmpty 时,我在 MSDN 上注意到了这一点: However, as this collection is intended to be accessed concurrently,
如何仅在满足某些条件时才将 ConcurrentQueue 的下一个元素出队? 例如如果要出队的下一个项目满足特定条件,则将其出队,否则离开。本质上是一个 'DequeueIf' 或 'TryDequ
如果队列中没有项目,ConcurrentQueue 中的 TryDequeue 将返回 false。 如果队列为空,我需要我的队列等待新项目添加到队列中并将新项目从队列中取出,然后该过程将继续进行。
我是一名优秀的程序员,十分优秀!