blockingqueue

Can I implement blocking queue using Semaphore in Java?

核能气质少年 提交于 2019-12-01 11:09:27
I wonder if it is possible to use Semaphore to implement blocking queue? In the below codes, I use one Semaphore to protect the critical section, and two more Semaphore objects to track the number of empty slots and filled objects. public class BlockingQueue { private List<Object> queue = new LinkedList<Object>(); private int limit; private Semaphore slots; // semaphore for empty slots private Semaphore objs; // semaphore for filled slots private Semaphore mutex; // for the critical section public BlockingQueue(int limit) { this.limit = limit; this.slots = new Semaphore(limit); // initial

Can I implement blocking queue using Semaphore in Java?

蹲街弑〆低调 提交于 2019-12-01 08:50:36
问题 I wonder if it is possible to use Semaphore to implement blocking queue? In the below codes, I use one Semaphore to protect the critical section, and two more Semaphore objects to track the number of empty slots and filled objects. public class BlockingQueue { private List<Object> queue = new LinkedList<Object>(); private int limit; private Semaphore slots; // semaphore for empty slots private Semaphore objs; // semaphore for filled slots private Semaphore mutex; // for the critical section

When should I use SynchronousQueue

可紊 提交于 2019-11-29 18:59:46
new SynchronousQueue() new LinkedBlockingQueue(1) What is the difference? When I should use SynchronousQueue against LinkedBlockingQueue with capacity 1? the SynchronousQueue is more of a handoff, whereas the LinkedBlockingQueue just allows a single element. The difference being that the put() call to a SynchronousQueue will not return until there is a corresponding take() call, but with a LinkedBlockingQueue of size 1, the put() call (to an empty queue) will return immediately. I can't say that i have ever used the SynchronousQueue directly myself, but it is the default BlockingQueue used for

Thread-safety of BlockingQueue's drainTo() method

无人久伴 提交于 2019-11-29 17:16:39
问题 Documentation of BlockingQueue says bulk operations are not thread-safe, though it doesn't explicitly mention the method drainTo(). BlockingQueue implementations are thread-safe. All queuing methods achieve their effects atomically using internal locks or other forms of concurrency control. However, the bulk Collection operations addAll, containsAll, retainAll and removeAll are not necessarily performed atomically unless specified otherwise in an implementation. So it is possible, for example

C++ pthread blocking queue deadlock (I think)

你。 提交于 2019-11-29 00:16:06
I am having a problem with pthreads where i think i am getting a deadlock. I have created a blocking queue which I thought was working, but after doing some more testing I have found that if i try and cancel multiple threads that are blocking on the blocking_queue, i seem to get a deadlock. The blocking queue is very simple and looks like this: template <class T> class Blocking_Queue { public: Blocking_Queue() { pthread_mutex_init(&_lock, NULL); pthread_cond_init(&_cond, NULL); } ~Blocking_Queue() { pthread_mutex_destroy(&_lock); pthread_cond_destroy(&_cond); } void put(T t) { pthread_mutex

Equivalent of golang channel in Java

萝らか妹 提交于 2019-11-28 21:26:12
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 timeout will work, but that is not efficient at all as it still needs to keep looping over all the

ExecutorService vs ThreadPoolExecutor using LinkedBlockingQueue

不羁岁月 提交于 2019-11-28 17:10:54
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.submit(new NewTask()); } executor.shutdown(); executor.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS); } }

ScheduledExecutorService with variable delay

痞子三分冷 提交于 2019-11-28 08:17:00
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 Brian Agnew I don't think you can change a fixed rate delay. I think you need to use schedule() to perform a one-shot,

producer/consumer work queues

落爺英雄遲暮 提交于 2019-11-28 05:34:01
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 really happening is that I'm always fully draining my work queue and filling the task queue, which slowly

Java BlockingQueue take() vs poll()

流过昼夜 提交于 2019-11-28 05:22:19
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); } Blocking is likely more efficient. In the background, the thread that initially calls take() goes to sleep if there is no element available, letting other threads do whatever they need to do. The methods that add elements to the