blockingqueue

producer consumer - ExecutorService & ArrayBlockingQueue

回眸只為那壹抹淺笑 提交于 2019-12-12 13:27:59
问题 I wish to know if my understanding of the producer consumer design is correct by using the ExecutorService & ArrayBlockingQueue. I understand there are different ways to implement this design but I guess, at the end, it depends on the problem itself. The problem I had to confront is this: I have a ONE producer who reads from a big file (6GB); it reads line by line and converts every line to an object. It places the object in an ArrayBlockingQueue. The consumers (few) take the object from the

WinJS Promise based file upload queue

青春壹個敷衍的年華 提交于 2019-12-12 00:02:06
问题 Scenario I need a background upload queue that sends files to a server. The queue should send the files in sequential order as they are pushed into the queue (FIFO). My solution var pendingFiles = []; var filesOperation = null; uploadNextAsync = function(file) { var next; if (!pendingFiles.length) { return WinJS.Promise.as(); } next = pendingFiles.shift(); fileslogger.debug("Uploading " + next); return fileQuery.folder.getFileAsync(next).then(function(file) { return Server.sendFileAsync(file)

why is there a while loop in put() of LinkedBlockingQueue

会有一股神秘感。 提交于 2019-12-11 07:36:51
问题 public void put(E e) throws InterruptedException { if (e == null) throw new NullPointerException(); int c = -1; Node<E> node = new Node<E>(e); final ReentrantLock putLock = this.putLock; final AtomicInteger count = this.count; putLock.lockInterruptibly(); try { while (count.get() == capacity) { notFull.await(); } enqueue(node); c = count.getAndIncrement(); if (c + 1 < capacity) notFull.signal(); } finally { putLock.unlock(); } if (c == 0) signalNotEmpty(); } why is there a while loop? All the

Python: Kombu+RabbitMQ Deadlock - queues are either blocked or blocking

徘徊边缘 提交于 2019-12-10 13:21:49
问题 The problem I have a RabbitMQ Server that serves as a queue hub for one of my systems. In the last week or so, its producers come to a complete halt every few hours. What have I tried Brute force Stopping the consumers releases the lock for a few minutes, but then blocking returns. Restarting RabbitMQ solved the problem for a few hours. I have some automatic script that does the ugly restarts, but it's obviously far from a proper solution. Allocating more memory Following cantSleepNow's

ArrayBlockingQueue: concurrent put and take

冷暖自知 提交于 2019-12-10 11:57:22
问题 Why ABQ has not been implemented using the way LinkedBlockingQueue. We can use AtomicInteger to keep Track count in ABQ also the same way LBQ does. We can use the Two Locks for ABQ too. I stumble upon the similar question on SO. ArrayBlockingQueue uses a single lock for insertion and removal but LinkedBlockingQueue uses 2 separate locks But I couldn't understand the answer for that question. I need help in understanding that problem which would arise if we implement ABQ using two locks. If

how to give priority to the threads waiting in a semaphore?

自作多情 提交于 2019-12-10 06:40:55
问题 I have used a semaphore to restrict the number of threads accessing a function. I want the thread to be awakened next should be chosen by some priority which i will be giving,not by default way that semaphore awaken's them ? How can we achieve this ? Here is the implementation : class MyMathUtil2 implements Runnable { double a; double b; String name = "demo"; Thread t; //static int currentCount = 0; static int MAX_COUNT = 2; private final Semaphore available = new Semaphore(MAX_COUNT, true);

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

烈酒焚心 提交于 2019-12-10 04:33:05
问题 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

Java BlockingQueue

冷暖自知 提交于 2019-12-10 04:01:49
前言 在新增的Concurrent包中,BlockingQueue很好的解决了多线程中,如何高效安全“传输”数据的问题。通过这些高效并且线程安全的队列类,为我们快速搭建高质量的多线程程序带来极大的便利。本文详细介绍了BlockingQueue家庭中的所有成员,包括他们各自的功能以及常见使用场景。 认识BlockingQueue 阻塞队列,顾名思义,首先它是一个队列,而一个队列在数据结构中所起的作用大致如下图所示: 从上图我们可以很清楚看到,通过一个共享的队列,可以使得数据由队列的一端输入,从另外一端输出; 常用的队列主要有以下两种:(当然通过不同的实现方式,还可以延伸出很多不同类型的队列,DelayQueue就是其中的一种) 先进先出(FIFO) :先插入的队列的元素也最先出队列,类似于排队的功能。从某种程度上来说这种队列也体现了一种公平性。 后进先出(LIFO) :后插入队列的元素最先出队列,这种队列优先处理最近发生的事件。 多线程环境中,通过队列可以很容易实现数据共享,比如经典的“生产者”和“消费者”模型中,通过队列可以很便利地实现两者之间的数据共享。假设我们有若干生产者线程,另外又有若干个消费者线程。如果生产者线程需要把准备好的数据共享给消费者线程,利用队列的方式来传递数据,就可以很方便地解决他们之间的数据共享问题。但如果生产者和消费者在某个时间段内

BlockingQueue学习

徘徊边缘 提交于 2019-12-10 03:37:04
引言 在Concurrent包中,BlockingQueue很好的解决了在多线程中,如何高效安全“传输”数据的问题。通过这些高效并且线程安全的队列类,为我们快速搭建高质量的多线程程序带来极大的便利。同时,BlockingQueue也用于java自带线程池的缓冲队列中,了解BlockingQueue也有助于理解线程池的工作模型。 一 BlockingQueue接口 该接口属于队列,所以继承了Queue接口,该接口最重要的五个方法分别是offer方法,poll方法,put方法,take方法和drainTo方法。 offer方法和poll方法分别有一个静态重载方法,分别是offer(E e, long timeout, TimeUnit unit)和poll(long timeout, TimeUnit unit)方法。其意义是在限定时间内存入或取出对象,如果不能存入取出则返回false。 put方法会在当队列存储对象达到限定值时阻塞线程,而在队列不为空时唤醒被take方法所阻塞的线程。take方法是相反的。 drainTo方法可批量获取队列中的元素。 二 常见的BlockingQueue实现 一 LinkedBlockingQueue LinkedBlockingQueue是比较常见的BlockingQueue的实现,他是基于链表的阻塞队列。在创建该对象时如果不指定可存储对象个数大小时

java BlockingQueue does not have a blocking peek?

ε祈祈猫儿з 提交于 2019-12-09 07:39: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.