blockingqueue

Blocking queue and multi-threaded consumer, how to know when to stop

杀马特。学长 韩版系。学妹 提交于 2019-11-27 16:58:10
I have a single thread producer which creates some task objects which are then added into an ArrayBlockingQueue (which is of fixed size). I also start a multi-threaded consumer. This is build as a fixed thread pool ( Executors.newFixedThreadPool(threadCount); ). I then submit some ConsumerWorker intances to this threadPool, each ConsumerWorker having a refference to the above mentioned ArrayBlockingQueue instance. Each such Worker will do a take() on the queue and deal with the task. My issue is, what's the best way to have a Worker know when there won't be any more work to be done. In other

Equivalent of golang channel in Java

旧巷老猫 提交于 2019-11-27 13:59:50
问题 I have a requirement where I need to read from a set of Blocking queues. The blocking queues are created by the Library I am using. My code has to read from the queues. I don't want to create a reader thread for each of these blocking queues. Rather I want to poll them for availability of data using a single thread (or probably using 2/3 threads at max). As some of the blocking queues might not have data for long time, while some of them may get bursts of data. Polling the queues with small

ExecutorService vs ThreadPoolExecutor using LinkedBlockingQueue

喜欢而已 提交于 2019-11-27 10:08:08
问题 I am working on a multithreaded project in which I need to spawn multiple threads to measure the end to end performance of my client code, as I'm doing Load and Performance testing. So I created the below code which is using ExecutorService . Below is the code with ExecutorService : public class MultithreadingExample { public static void main(String[] args) throws InterruptedException { ExecutorService executor = Executors.newFixedThreadPool(20); for (int i = 0; i < 100; i++) { executor

Are there any concurrent containers in C++11? [closed]

▼魔方 西西 提交于 2019-11-27 10:06:01
问题 In particular, I am looking for a blocking queue. Is there such a thing in C++11? If not, what are my other options? I really don't want to go down to the thread level myself anymore. Way too error-prone. 回答1: According to Diego Dagum from Microsoft's Visual C++ Team: A recurrent question (well, one of the many) is about STL containers and whether they are thread safe. Taking Stephan’s words here, the reality is that they aren’t, not as a bug but as a feature: having every member function of

ScheduledExecutorService with variable delay

偶尔善良 提交于 2019-11-27 05:44:50
问题 Suppose I have a task that is pulling elements from a java.util.concurrent.BlockingQueue and processing them. public void scheduleTask(int delay, TimeUnit timeUnit) { scheduledExecutorService.scheduleWithFixedDelay(new Task(queue), 0, delay, timeUnit); } How can I schedule / reschedule the task if the frequency can be changed dynamically? The idea is to take a stream of data updates and propagate them in batch to a GUI The user should be able to vary the frequency of updates 回答1: I don't

ArrayBlockingQueue uses a single lock for insertion and removal but LinkedBlockingQueue uses 2 separate locks

被刻印的时光 ゝ 提交于 2019-11-27 01:58:03
问题 I was going through the source code of ArrayBlockingQueue and LinkedBlockingQueue. LinkedBlockingQueue has a putLock and a takeLock for insertion and removal respectively but ArrayBlockingQueue uses only 1 lock. I believe LinkedBlockingQueue was implemented based on the design described in Simple, Fast, and Practical Non-Blocking and Blocking Concurrent Queue Algorithms. In this paper, they mention that they keep a dummy node so that enqueuers never have to access head and dequeuers never

Java BlockingQueue take() vs poll()

六月ゝ 毕业季﹏ 提交于 2019-11-27 00:59:14
问题 When consuming values from a Queue in an infinite loop -- what would be more efficient: 1) Blocking on the Queue until a value is available via take() while (value = queue.take()) { doSomething(value); } 2) Sleeping for n milliseconds and checking if an item is available while (true) { if ((value = queue.poll()) != null) { doSomething(value); } Thread.sleep(1000); } 回答1: Blocking is likely more efficient. In the background, the thread that initially calls take() goes to sleep if there is no

producer/consumer work queues

痴心易碎 提交于 2019-11-27 00:58:58
问题 I'm wrestling with the best way to implement my processing pipeline. My producers feed work to a BlockingQueue. On the consumer side, I poll the queue, wrap what I get in a Runnable task, and submit it to an ExecutorService. while (!isStopping()) { String work = workQueue.poll(1000L, TimeUnit.MILLISECONDS); if (work == null) { break; } executorService.execute(new Worker(work)); // needs to block if no threads! } This is not ideal; the ExecutorService has its own queue, of course, so what's

Blocking queue and multi-threaded consumer, how to know when to stop

冷暖自知 提交于 2019-11-26 22:30:18
问题 I have a single thread producer which creates some task objects which are then added into an ArrayBlockingQueue (which is of fixed size). I also start a multi-threaded consumer. This is build as a fixed thread pool ( Executors.newFixedThreadPool(threadCount); ). I then submit some ConsumerWorker intances to this threadPool, each ConsumerWorker having a refference to the above mentioned ArrayBlockingQueue instance. Each such Worker will do a take() on the queue and deal with the task. My issue

How to get the ThreadPoolExecutor to increase threads to max before queueing?

ぐ巨炮叔叔 提交于 2019-11-26 14:06:29
I've been frustrated for some time with the default behavior of ThreadPoolExecutor which backs the ExecutorService thread-pools that so many of us use. To quote from the Javadocs: If there are more than corePoolSize but less than maximumPoolSize threads running, a new thread will be created only if the queue is full . What this means is that if you define a thread pool with the following code, it will never start the 2nd thread because the LinkedBlockingQueue is unbounded. ExecutorService threadPool = new ThreadPoolExecutor(1 /*core*/, 50 /*max*/, 60 /*timeout*/, TimeUnit.SECONDS, new