blockingqueue

what's wrong with my sensor monitoring technique?

こ雲淡風輕ζ 提交于 2019-12-09 04:48:41
问题 (please read UPDATE 3 at the end)I'm developing an app that continually works with the sensors of device, works with Accelerometer and Magnetic sensors to retrieve the orientation of device(the purpose is mentioned here). in other words, my app needs to know the orientation of device in Real-time(however this is never possible, so as fast as possible instead, but really as fast as possible !). as mentioned in professional Android 4 Application Development by Reto Meier : The accelerometers

Bash queue - atomic wait & lock

情到浓时终转凉″ 提交于 2019-12-08 10:23:26
问题 I want to use a queue in Bash, which one process (eg. one Bash script) will be writing to and several other processes will be reading from. I want to do this: writer: # block if another process is reading or writing, # then lock the queue using an exclusive lock lock -ex queue echo "$message" >> queue unlock queue reader: # wait until there is an item in the queue # and then lock the queue using a shared lock wait_while_empty_and_lock -sh queue read -r message < <(tail -n+1 queue) (do stuff..

理解BlockingQueue

爷,独闯天下 提交于 2019-12-07 15:27:39
在Concurrent包中,BlockingQueue很好的解决了多线程中,如何高效安全“传输”数据的问题。通过这些高效并且线程安全的队列类,为我们快速搭建高质量的多线程程序带来极大的便利。 what is BlockingQueue 阻塞队列,顾名思义,首先它是一个队列,而一个队列在数据结构中所起的作用大致如下图所示: 通过一个共享的队列,可以使得数据由队列的一端输入,从另外一端输出; 常用的队列主要有以下两种:(当然通过不同的实现方式,还可以延伸出很多不同类型的队列,DelayQueue就是其中的一种) 先进先出(FIFO) :先插入的队列的元素也最先出队列,类似于排队的功能。从某种程度上来说这种队列也体现了一种公平性。 后进先出(LIFO) :后插入队列的元素最先出队列,这种队列优先处理最近发生的事件。 多线程环境中,通过队列可以很容易实现数据共享,比如经典的“生产者”和“消费者”模型中,通过队列可以很便利地实现两者之间的数据共享。假设我们有若干生产者线程,另外又有若干个消费者线程。如果生产者线程需要把准备好的数据共享给消费者线程,利用队列的方式来传递数据,就可以很方便地解决他们之间的数据共享问题。但如果生产者和消费者在某个时间段内,万一发生数据处理速度不匹配的情况呢?理想情况下,如果生产者产出数据的速度大于消费者消费的速度,并且当生产出来的数据累积到一定程度的时候

Why can race condition in LogWriter cause the producer to block? [Concurrency in practice]

我只是一个虾纸丫 提交于 2019-12-07 12:36:49
问题 First of all to prevent mark question as duplicate by guys who don't like read to the end I have read Producer-Consumer Logging service with Unreliable way to shutdown question. But it is not fully answer the question and answer contradicts the book text. In book provided following code: public class LogWriter { private final BlockingQueue<String> queue; private final LoggerThread logger; private static final int CAPACITY = 1000; public LogWriter(Writer writer) { this.queue = new

How to Block a Queue in ForkJoinPool?

不想你离开。 提交于 2019-12-07 11:52:29
问题 I need to block threads on ForkJoinPool when its queue is full. This can be done in the standard ThreadPoolExecutor, e.g.: private static ExecutorService newFixedThreadPoolWithQueueSize(int nThreads, int queueSize) { return new ThreadPoolExecutor(nThreads, nThreads, 5000L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(queueSize, true), new ThreadPoolExecutor.CallerRunsPolicy()); } I know, there is some Dequeue inside ForkJoinPool, but I don't have access to it via its API. Update:

Suspend consumer in producer/consumer pattern

允我心安 提交于 2019-12-07 02:39:57
问题 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 =

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

元气小坏坏 提交于 2019-12-06 14:52:53
问题 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

notify() instead of notifyAll() for blocking queue

*爱你&永不变心* 提交于 2019-12-06 12:04:21
问题 I am trying to find out whether it is possible to have a multiple producer / multiple consumer queue where I can use notify() instead of notifyAll() . For example, in the implementation below (source: here) you cannot just simply switch the notifyAll() for notify() . It is not totally obvious why you cannot switch so I will leave it as an teaser to whoever wants to help me out understanding this problem. So the code below is broken: public class BlockingQueue { private Object lock = new

How to Block a Queue in ForkJoinPool?

╄→гoц情女王★ 提交于 2019-12-06 00:32:29
I need to block threads on ForkJoinPool when its queue is full. This can be done in the standard ThreadPoolExecutor, e.g.: private static ExecutorService newFixedThreadPoolWithQueueSize(int nThreads, int queueSize) { return new ThreadPoolExecutor(nThreads, nThreads, 5000L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(queueSize, true), new ThreadPoolExecutor.CallerRunsPolicy()); } I know, there is some Dequeue inside ForkJoinPool, but I don't have access to it via its API. Update: Please see the answer below. After some research I am happy to answer the question: Reason: There is no

Being asynchronously notified of a BlockingQueue having an item available

大憨熊 提交于 2019-12-05 18:27:59
问题 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