blockingqueue

Java: Thread producer consumer what is the most efficient way to wait for data to be produced

陌路散爱 提交于 2019-12-05 06:37:45
When using BlockingQueue to consume data that is produced what is the most efficient method for waiting for the data to appear? Scenario: Step 1) The data list will be a data store where timestamps are added to. These timestamps are required to be ordered by closest to current time priority. This list may be empty. A thread will be inserting the timestamps into it. Produce Step 2) I want to consume the data in here in another thread that will take the timestamps from data and check if they are after the current time. Consumer then Produce Step 3) If they are after the current time then send

Suspend consumer in producer/consumer pattern

≯℡__Kan透↙ 提交于 2019-12-05 05:56:15
I have producer and consumer connected with BlockingQueue . Consumer wait records from queue and process it: Record r = mQueue.take(); process(r); I need pause this process for a while from other thread. How to implement it? Now I think implement it such, but it's looks like bad solution: private Object mLock = new Object(); private boolean mLocked = false; public void lock() { mLocked = true; } public void unlock() { mLocked = false; mLock.notify(); } public void run() { .... Record r = mQueue.take(); if (mLocked) { mLock.wait(); } process(r); } I think your solution is simple and elegant,

How to immediately release threads waiting on a BlockingQueue

蹲街弑〆低调 提交于 2019-12-05 02:50:01
Consider a BlockingQueue and a few threads waiting on poll(long, TimeUnit) (possibly also on on take() ). Now the queue is empty and it is desired to notify the waiting threads that they can stop waiting. The expected behaviour is to have either null returned or the declared InterruptedException thrown. Object.notify() won't work for LinkedBlockingQueue as the threads are waiting on an internal lock. Any straightforward way? Javadoc for the BlockingQueue suggests a good way: A BlockingQueue does not intrinsically support any kind of "close" or "shutdown" operation to indicate that no more

Java: Producer/Consumer using BlockingQueue: having the consumer thread wait() until another object is queued

不羁的心 提交于 2019-12-04 20:46:52
I've been having some thread related problems recently with a consumer that takes points. Here is the original, which works fine except for taking up a lot of cpu constantly checking the queue. The idea is that cuePoint can be called casually and the main thread keeps going. import java.util.List; import java.util.ArrayList; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; public class PointConsumer implements Runnable { public static final int MAX_QUEUE_SIZE=500; BlockingQueue<Point> queue; public PointConsumer (){ this.queue=new ArrayBlockingQueue

What is the Difference between ArrayBlockingQueue and LinkedBlockingQueue

断了今生、忘了曾经 提交于 2019-12-04 07:43:11
问题 What scenarios is it better to use an ArrayBlockingQueue and when is it better to use a LinkedBlockingQueue? If LinkedBlockingQueue default capacity is equal to MAX Integer, is it really helpful to use it as BlockingQueue with default capacity? 回答1: ArrayBlockingQueue is backed by an array that size will never change after creation. Setting the capacity to Integer.MAX_VALUE would create a big array with high costs in space. ArrayBlockingQueue is always bounded. LinkedBlockingQueue creates

Being asynchronously notified of a BlockingQueue having an item available

五迷三道 提交于 2019-12-04 02:51:44
I need an Object to be asynchronously notified when some BlockingQueue has got an item to give. I've searched both Javadoc and the web for a pre-made solution, then I ended up with a (maybe naive) solution of mine, here it is: interface QueueWaiterListener<T> { public void itemAvailable(T item, Object cookie); } and class QueueWaiter<T> extends Thread { protected final BlockingQueue<T> queue; protected final QueueWaiterListener<T> listener; protected final Object cookie; public QueueWaiter(BlockingQueue<T> queue, QueueWaiterListener<T> listener, Object cookie) { this.queue = queue; this

Queue Full, On depth of the Blocking Queue, clarification needed

戏子无情 提交于 2019-12-03 23:43:42
When populating the queue from the contents of the file, depth does not seem to ever increase, as elements are not added in this implementation. BlockingQueue<String> q = new SynchronousQueue<String>(); ... fstream = new FileInputStream("/path/to/file.txt"); ... while ((line = br.readLine()) != null) { if (q.offer(line)) System.out.println("Depth: " + q.size()); //0 } When replacing offer with add , exception if thrown Exception in thread "main" java.lang.IllegalStateException: Queue full ... What am i doing wrong please? Why is the queue full immediately, upon insertion of the first element?

Multiple Producer Multiple Consumer Multithreading Java

有些话、适合烂在心里 提交于 2019-12-03 21:01:19
I'm trying out Multiple Producer - Multiple Consumer use case of Producer-Consumer problem. I'm using BlockingQueue for sharing common queue between multiple producers/consumers. Below is my code. Producer import java.util.concurrent.BlockingQueue; public class Producer implements Runnable { private BlockingQueue inputQueue; private static volatile int i = 0; private volatile boolean isRunning = true; public Producer(BlockingQueue q){ this.inputQueue=q; } public synchronized void run() { //produce messages for(i=0; i<10; i++) { try { inputQueue.put(new Integer(i)); Thread.sleep(1000); } catch

BlockingQueue-线程的阻塞队列

随声附和 提交于 2019-12-03 16:15:19
BlockingQueue作为线程容器,可以为线程同步提供有力的保障,其主要用到的方法包括: [java] add(E o); //将指定的元素添加到此队列中(如果立即可行),在成功时返回 true,其他情况则抛出 IllegalStateException。 drainTo(Collection<? super E> c); //移除此队列中所有可用的元素,并将它们添加到给定 collection 中。 drainTo(Collection<? super E> c,int maxElements);//最多从此队列中移除给定数量的可用元素,并将这些元素添加到给定 collection 中 offer(E o); //如果可能的话,将指定元素插入此队列中。 offer(E o, long timeout, TimeUnit unit); //将指定的元素插入此队列中,如果没有可用空间,将等待指定的等待时间(如果有必要)。 poll(long timeout, TimeUnit unit); //检索并移除此队列的头部,如果此队列中没有任何元素,则等待指定等待的时间(如果有必要)。 put(E o); //将指定元素添加到此队列中,如果没有可用空间,将一直等待(如果有必要)。 remainingCapacity(); //返回在无阻塞的理想情况下(不存在内存或资源约束

java BlockingQueue does not have a blocking peek?

二次信任 提交于 2019-12-03 09:25:29
I have a blocking queue of objects. I want to write a thread that blocks till there is a object on the queue. Similar to the functionality provided by BlockingQueue.take(). However, since I do not know if I will be able to process the object successfully, I want to just peek() and not remove the object. I want to remove the object only if I am able to process it successfully. So, I would like a blocking peek() function. Currently, peek() just returns if the queue is empty as per the javadocs. Am I missing something? Is there another way to achieve this functionality? EDIT: Any thoughts on if I