Java multithreading for consumer manufacturers

I am following the video tutorial on Java multithreading. He introduces the use of Java to implement the well-known consumer producer problem.

He used wait () and notifyAll () to ensure proper communication between producer threads and consumer threads.

The mentor intentionally created several producer flows, while only one consumer flow, but he left unanswered: "It is always better to have an equal number of producer and consumer flows, if there are more producer flows than the consumer, to be problems."

However, he did not indicate what the problem was. I personally imagine that this will only be a situation when the basket is full. Can experts help here? Thank.

+5
source share
6 answers

You can have any number of manufacturers and any number of consumers.

If manufacturers produce too quickly, the queue will be filled until you have problems with memory, or the producers are forced to remain idle until there is some place in the queue.

If consumers consume too quickly, they will remain on standby most of the time.

+6
source

It is always better to have an equal number of producers and consumer flows

I personally do not agree with this.

How many manufacturers and how consumer flows are completely dependent on the situation. There is no direct rule for this.

I will tell you my business

, CSV , XML XML JMS Queue. , . , . , .

2 -

  • CSV XML

  • XML JMS.

, , . , , , , .

, , . , , , (), OutOfMemory extreame cases.

+2

, . , , / , .

. make -j, , n+1 n, ' .

+1

, , CPU . , (, ), , , .

, ( ) , .

/ . , , ( ..). , , , .

+1

@JB Nizet , . , , , . , , , , . , , , , , , .

The mentor probably believes that the consumption process is more efficient than the production process, which may be true in many, or even most cases, but this is not necessarily a good guess. Just try to take this into account when developing producer / consumer systems.

+1
source

I think that this does not depend on the number of consumers / producers, but on speed in the first place. You can have 1 consumer who can process 1 message in 10 ms, and 2 manufacturers produce 1 message of 100 ms each.

+1
source

All Articles