producer-consumer

Java thread wait and notify

我与影子孤独终老i 提交于 2019-12-09 10:09:06
问题 I have two threads. Thread A is pulling some elements from queue and thread B is adding some elements to the queue. I want thread A to go to sleep when the queue is empty. When thread B adds some element to the queue it should make sure that thread A is working. How can this be done in Java? 回答1: Use a BlockingQueue, which is: A Queue that additionally supports operations that wait for the queue to become non-empty when retrieving an element, and wait for space to become available in the

Implementing Producer consumer pattern

ぃ、小莉子 提交于 2019-12-09 01:02:04
问题 I am trying to write a mail utility that places mails in a queue, and it is later consumed by a consumer thread. I am trying to implement a typical producer-consumer pattern, but something is going wrong. I just wrote a skeleton , and the skeleton is not working as expected. MailProducer.java public class MailProducer implements Callable<Void> { @Override public Void call() throws Exception { System.out.println("inside mail Producer"); System.out.println("Thread executing = " + Thread

Producer-consumer using semaphore in C

走远了吗. 提交于 2019-12-08 13:53:55
问题 There is producer-consumer problem is written using semaphore. In below code, there is an issue of synchronization execution while the consumer is created. And for its solution, sleep statement is added in switch block of the consumer. Kindly help me with the efficient solution for the synchronization. Any suggestion for the improvement of code is really helpful. #include<stdio.h> #include<pthread.h> #include<unistd.h> #include<semaphore.h> /* sem_t */ #include<stdlib.h> #define BUF_SIZE 2

Consumer doesn't work in my simple producer/consumer/queue code in Java

此生再无相见时 提交于 2019-12-08 12:53:39
I am trying to implement a simple producer/consumer system in Java 11. Basically, I take two threads for each, plus a global queue, simply as follows: A global priority queue. The first thread, producer, runs a HTTP server, listens to incoming http messages, and upon receiving a message, pushes it as a job to the queue ( queue.size increments) The second thread, the consumer, continously peeks the queue. If there is a job ( job ! = null ), submits a HTTP request somewhere and upon successful receipt, polls it from the queue ( queue.size() decrements). The skeleton is as below: Main Class:

Proper implementation of producer-consumer scenario and “graceful” termination of thread pool

删除回忆录丶 提交于 2019-12-08 10:44:21
问题 I am working on my first multi-threaded project and thus have a couple of things that I am unsure of. Details on my setup was on a previous question, in short: I have a thread pool implemented by Executors.newFixedThreadPool(N) . One thread is given an action which does a series of queries to local and remote resources and iteratively populates an ArrayBlockingQueue , while the rest of the threads invoke take() method on the queue and process the objects in the queue. Even though small and

Producer consumer code with wait/notify doesn't work on second produce

戏子无情 提交于 2019-12-08 08:17:38
问题 This is a follow-up question from my previous question asked here. I am using a PriorityBlockingQueue now. I changed my producer to the following: synchronized(Manager.queue) { Manager.queue.add(new Job()); Manager.queue.notify(); } And changed Consumer to the following. Full code skeleton is here: //my consumer thread run() public void run() { synchronized(Manager.queue) { while (Manager.queue.peek() == null) { System.out.println("111111111111111"); try { Manager.queue.wait(); } catch

Producer Consumer in Java with a UI Toggle

[亡魂溺海] 提交于 2019-12-08 05:28:07
问题 I am trying, and failing, to implement a Producer Consumer pattern in java, subject to the following constraints: The Producer produces into (and the Consumer consumes from) a queue with finite size There is a user interface with buttons toggling the Producer and Consumer, separately The Producer should produce only when queue is not full and the producer button is toggled active, The Consumer should consume only when the queue is not empty and the consumer button is toggled active. Both

Queueing Multiple Downloads, looking for a producer consumer API

荒凉一梦 提交于 2019-12-08 05:13:28
问题 I have an application (a servlet, but that's not very important) that downloads a set of files and parse them to extract information. Up to now, I did those operations in a loop : - fetching new file on the Internet - analyzing it. A multi-threaded download-manager seems a better solution for this problem and I would like to implement it in the fastest way possible. Some of the downloads are dependant from others (so, this set is partially ordered). Mutli-threaded programming is hard and if I

Kafka consumer not able to consume messages using bootstrap server name

爱⌒轻易说出口 提交于 2019-12-08 04:41:27
I am facing an issue while consuming message using the bootstrap-server i.e. Kafka server. Any idea why is it not able to consume messages without zookeeper? Kafka Version: kafka_2.11-1.0.0 Zookeeper Version: kafka_2.11-1.0.0 Zookeeper Host and port: zkp02.mp.com:2181 Kafka Host and port: kfk03.mp.com:9092 Producing some message: [kfk03.mp.com ~]$ /bnsf/kafka/bin/kafka-console-producer.sh --broker-list kfk03.mp.com:9092 --topic test >hi >hi Consumer not able to consume messages if I give –-bootstrap-server : [kfk03.mp.com ~]$ /bnsf/kafka/bin/kafka-console-consumer.sh --bootstrap-server kfk03

Producer Consumer Program using wait() and notify() in Java

雨燕双飞 提交于 2019-12-08 04:13:51
问题 I am doing classic Producer-Consumer problem in Java using low level synchronization and wait() and notify(). I know there are better implementations using structures from java.util.concurrent package but my problem revolves around low level implementation: private static ArrayList<Integer> list = new ArrayList<Integer>(); static Object obj = new Object(); public static void producer() throws InterruptedException { synchronized (obj) { while (true) { if (list.size() == 10) { System.out