问题标签 [blockingqueue]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
java - 轮询会阻塞 LinkedBlockingQueue 中的其他操作吗?
在下面的伪代码中,我有一个poll()
在主线程中被永久调用的函数。当我在没有sleep()
语句的情况下执行此操作时poll()
,其他线程每分钟仅将 2-3 个项目添加到队列中。这是否意味着轮询会阻止该put()
语句?
我怎么解决这个问题?
java - 将 Java 应用程序移植到 C++ (qt)
我已经使用 Swing 用 Java 编写了应用程序,现在我正在尝试用 C++ 重写它。Java 中的程序有控制器,它有对事件的模型、视图和 BlockingQueue 的引用。当 View 发生某些事情时,新事件被放入 BlockingQueue 并由 Controller 和 Model 处理。然后 SwingUtilities.invokeLater() 在 Swing 中调用了一些动作。
如何使用 Qt 在 C++ 中做这样的事情?我已经编写了模型,但我不知道如何通过 Java 中的 BlockingQueue 之类的东西将它与用 Qt 编写的 UI 连接起来。
java - 在 Quartz 的 JobDataMap 中传递 BlockingQueue
有没有办法将 BlockingQueue 传递给 Quartz 框架中的作业?我尝试使用 JobDataMap 来传递 BlockingQueue 但这似乎不起作用。这里是相关的代码片段:
也许有人对如何实现这一目标有想法。
java - ArrayBlockingQueue 使用单个锁进行插入和删除,但 LinkedBlockingQueue 使用 2 个单独的锁
我正在浏览 ArrayBlockingQueue 和 LinkedBlockingQueue 的源代码。LinkedBlockingQueue 有一个 putLock 和一个 takeLock 分别用于插入和删除,但 ArrayBlockingQueue 只使用 1 个锁。我相信 LinkedBlockingQueue 是基于Simple, Fast, and Practical Non-Blocking and Blocking Concurrent Queue Algorithms中描述的设计实现的。在本文中,他们提到他们保留了一个虚拟节点,以便入队者永远不必访问头,而出队者永远不必访问尾,从而避免了死锁情况。我想知道为什么 ArrayBlockingQueue 不借用相同的想法而是使用 2 个锁。
java - BlockingQueue java 的问题,Storm 的实现(分布式计算)?
这是我的输入 spout 的代码片段,用于将元组发送到一个处理节点,以便在集群上进行流处理。问题是 BlockingQueue 正在抛出 InterruptedException 。
异常描述如下:---
java.lang.InterruptedException10930 [Thread-20] INFO backtype.storm.util - 异步循环中断!
而 nextTuple(InputStreamSpout.java:65 是 ------>
谢谢
java - 带有消费者过滤的生产者-消费者阻塞队列
我正在尝试在 java 中编写生产者-消费者模式。我是一个网络客户端,通过一系列不同的连接连接到许多服务器。服务器产生不同的“工作”数据包,这些数据包都被放入一个阻塞队列。我想要做的是以一种只有特定类型的工作包才能交付给特定消费者的方式阻止我的消费者。
例子:
- 阻塞队列中填充了三种不同类型的工作包对象:A、B、C
- 两个消费者正在等待工作包对象。消费者 1 只想要 A 型,消费者 2 只想要 B 型或 C 型
java.util 中是否有一些标准类...或者我必须推出自己的类?另外,如果需要的话,最好的方法是什么?
提前致谢!
安德鲁·克洛法斯
java - 任何允许组合条目的有效 BlockingQueue 实现?
java.util.concurrent.BlockingQueue 是否有任何有效的(不同步所有内容)实现允许组合条目?
通过组合,我的意思是将传入项目与队列中现有的“相等”条目(如果有的话)合并,否则像往常一样在末尾添加项目。
java - 具有唯一 ID 的 ArrayBlockingQueue
在从 StackOverflow 获得帮助后,我找到了我在下面实现的解决方案。
问题陈述:-
每个线程每次都需要使用UNIQUE ID
,并且必须运行60 minutes
或更长时间,因此60 minutes
有可能所有线程都ID's
将完成,因此我需要ID's
再次重用它们。所以我在ArrayBlockingQueue
这里使用概念。
两种情况:-
- 如果
command.getDataCriteria()
包含Previous
,那么每个线程总是需要在UNIQUE ID
之间使用1 and 1000
并释放它以再次重用。 - 否则,如果
command.getDataCriteria()
包含New
,那么每个线程总是需要在UNIQUE ID
之间使用2000 and 3000
并释放它以再次重用。
问题:-
我刚刚注意到的一件奇怪的事情是 - 在下面的 else if 循环中,如果你在 run 方法中看到我的下面的代码,command.getDataCriteria() is Previous
那么它也被输入到else if block(which is for New)
其中不应该发生的事情,因为我正在做一个.equals check
?为什么会这样?
以下是我的代码: -
更新:-
我刚刚注意到的一件奇怪的事情是 - 在下面的else if loop
if command is Previous
then 中,它也被输入到 else if 块中,这不应该发生对吗?为什么会这样?我不知道为什么会发生这种情况?
ios - On iOS, how to implement blocking or "being blocked" on a thread that produces data?
If we need to implement a queue (being done by NSMutableArray
), and we have Producer
objects that add data to the queue. There can be 1 producer or multiple producers. And if the queue is full (at size of 100, for example), the thread should be "blocked" (waiting, until the queue is actually smaller size than 100 items). (the same goes for taking away data from the queue: when it is empty, the getting of data is also blocked).
How can this "blocking" be implemented? Right now I use a Mutex
so that the count of the NSMutableArray is obtained and adding of item is done together (because the count can be 99, but when the item is added, the count can already be different).
But for the blocking effect, I actually check the count, and if 100, release the lock and sleep for 0.1 second, and then get the lock again and get the count, and repeat as above.
Is this a good way to implement the "blocking" behavior? There seems to be another way to get a canAddNowLock
, which is simply blocking by the NSLock
mechanism, and when a queue item is removed, then release this canAddNowLock
, but since there can be multiple data producers and data consumers, what if several producers are unblocked, or what if multiple consumers unlock canAddNowLock
in a row and only 1 producer is unblocked (when in fact several producers should be unblocked)? It just seem a more complicated design that way.
java - “并发应用程序中 LinkedBlockingQueue 的不可预测性能”是什么意思?
对于我正在处理的日志记录功能,我需要一个处理线程,它会等待作业并在计数达到或超过一定数量时分批执行它们。由于这是生产者消费者问题的标准案例,我打算使用BlockingQueues。我有许多生产者使用add()方法将条目添加到队列中,而只有一个消费者线程使用take()来等待队列。
LinkedBlockingQueue似乎是一个不错的选择,因为它没有任何大小限制,但是我很困惑从文档中阅读此内容。
链接队列通常比基于数组的队列具有更高的吞吐量,但在大多数并发应用程序中性能更不可预测。
没有清楚地解释这句话的含义。有人可以照亮它吗?这是否意味着 LinkedBlockingQueue 不是线程安全的?你们中的任何人在使用 LinkedBlockingQueue 时遇到过任何问题吗?
由于生产者的数量要多得多,所以我总是会遇到这样一种情况,即队列被大量要添加的条目压得喘不过气来。如果我改用ArrayBlockingQueue,它将队列的大小作为构造函数中的参数,我总是会遇到与容量满相关的异常。为了避免这种情况,我不确定如何确定我应该用什么大小来实例化我的 ArrayBlockingQueue。您是否必须使用 ArrayBlockingQueue 解决类似的问题?