0

In a producer consumer pattern using ExecutorService what if all threads starts executing the consumer task and blocks themselves waiting for the producer to push data indefinitely?

Can this ever happen ?? If I have say X no of Url's to download and then parse the content and store in a database. I use producer and consumer analogy of multithreading to solve the problem.

The producer will download the content and place the data into a BlockingQueue of size Y (say Y

Now say I create a thread pool of N no of threads where N << X. Add I assign X number of producer task and X no of Consumer task.

In such as case if all threads execute the consumer task and gets blocked when it tries to collect data from the blocking queue.Then basically I am left with no Thread to execute the producer task and I am in a stalemate.

debanka
  • 187
  • 1
  • 4
  • 13
  • 1
    Yes, when you make such faulty configuration, you may run into the problems you asked for. What’s the question? – Holger Aug 23 '18 at 16:31
  • My question is if I try to implement producer consumer pattern using executor service where I don't have any control over threads. Then how can I avoid this problem? – debanka Aug 24 '18 at 05:02
  • It is impossible without any control. What if the executor has only one thread? The minimum you need, is knowledge about the executor’s policies, including the minimum number of threads. Anything else depends on your actual application design. Saying that it is some sort of producer/consumer, is not enough information. – Holger Aug 24 '18 at 06:48

0 Answers0