blockingqueue

How to immediately release threads waiting on a BlockingQueue

无人久伴 提交于 2019-12-22 04:07:31
问题 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? 回答1: Javadoc for the BlockingQueue suggests a good way: A

How to immediately release threads waiting on a BlockingQueue

跟風遠走 提交于 2019-12-22 04:07:03
问题 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? 回答1: Javadoc for the BlockingQueue suggests a good way: A

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

一个人想着一个人 提交于 2019-12-21 07:09:05
问题 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 ...

“Closing” a blocking queue

旧巷老猫 提交于 2019-12-20 08:19:11
问题 I’m using java.util.concurrent.BlockingQueue in a very simple producer-consumer scenario. E.g. this pseudo code depicts the consumer part: class QueueConsumer implements Runnable { @Override public void run() { while(true) { try { ComplexObject complexObject = myBlockingQueue.take(); //do something with the complex object } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } } So far so good. In the javadoc of the blocking queue I read: A BlockingQueue does not

LinkedBlockingQueue thowing InterruptedException

旧巷老猫 提交于 2019-12-20 07:22:32
问题 I have this piece of code. A LinkedBlockingQueue should only throw an Exception if interrupted while waiting to add to the queue. But this queue is unbounded so it should add asap. Why does my shutdown methode throw an InterruptedException ? private final LinkedBlockingQueue<Message> messages= new LinkedBlockingQueue<Message>(); public void run(){ LinkedList<Message> messages = new LinkedList<Message>(); while (true){ try{ messages.clear(); messages.add(this.messages.take()); this.messages

BlockingQueue vs PipedOutputStream and PipedInputStream

不打扰是莪最后的温柔 提交于 2019-12-19 11:19:10
问题 I want to know Advantages to use BlockingQueue instead of ( PipedOutputStream and PipedInputStream ) import java.io.*; import java.util.concurrent.*; public class PipedStreamVsBlocking { public static void main(String... args) { BlockingQueue<Integer> blockingQueue = new LinkedBlockingDeque<>(2); ExecutorService executor = Executors.newFixedThreadPool(4); Runnable producerTask = () -> { try { while (true) { int value = ThreadLocalRandom.current().nextInt(0, 1000); blockingQueue.put(value);

How to wait on multiple blocking queues in parallel?

邮差的信 提交于 2019-12-19 02:21:10
问题 I have two separated blocking queues. The clients usually use either the first of the second blocking queue to retrieve elements to be processed. In some case, the clients are interested in elements from the two blocking queues, whichever queue provides the data first. How can a client wait for the two queues, in parallel? 回答1: You could try using the poll method in some sort of loop to only wait a specified amount of time for one queue before polling the other one. Other than that, I'd say

How to wait on multiple blocking queues in parallel?

微笑、不失礼 提交于 2019-12-19 02:21:10
问题 I have two separated blocking queues. The clients usually use either the first of the second blocking queue to retrieve elements to be processed. In some case, the clients are interested in elements from the two blocking queues, whichever queue provides the data first. How can a client wait for the two queues, in parallel? 回答1: You could try using the poll method in some sort of loop to only wait a specified amount of time for one queue before polling the other one. Other than that, I'd say

Asynchronous execution in BlockingQueue

强颜欢笑 提交于 2019-12-13 19:33:30
问题 I am using Java collections BlockingQueue for data processing. The current code looks like while(true){ if(queue.size() > 0) handle(queue.take()) } Is there a way (in Java or other frameworks) where I can process it asynchronously, like, queue.setHandler(new Handler<E>()); somewhere down the class.. class Handler implements IHandler<E>{ void handle(E e){ handle(e) } } 来源: https://stackoverflow.com/questions/32793159/asynchronous-execution-in-blockingqueue

Dequeue from Queue with where expression

浪子不回头ぞ 提交于 2019-12-13 03:29:28
问题 I have a blocking queue class based on Marc Gravell's Blocking Queue Now I have situations where I want to dequeue only a certain object, I know this is not really the use case of a queue, but in some cases I think this is a good extension, for example waiting for a certain network answer. This would be something like a TryDequeueWhere(Func<T, bool> expression, out T value, int? waitTimeInMs = null) The problem is I don't know how to wait and block for a certain object. 回答1: After posting my