Producer/Consumer Mode Blocking Queue LinkedBlockingQueue

[Refer to multiple articles, add my own understanding, the source code changes greatly]

 

Just abstracting away producers and consumers is not enough of a producer/consumer model. The pattern also requires a buffer between the producer and the consumer, acting as an intermediary. The producer puts data into the buffer, and the consumer takes data from the buffer

 

◇ Decoupling 
  assumes that the producer and the consumer are two classes, respectively. If the producer directly calls a method of the consumer, the producer will have a dependency on the consumer (that is, coupling). If the consumer's code changes in the future, it may affect the producer. And if both depend on a buffer, there is no direct dependence between the two, and the coupling is correspondingly reduced. 
   
◇ Supporting concurrent (concurrency) 
  producers to directly call a method of consumers has another drawback. Since the function call is synchronous (or blocking), the producer has to wait until the consumer's method returns. In case consumers are slow to process data, producers will waste their time. 
  After using the producer/consumer model, the producer and the consumer can be two independent concurrent subjects (the common concurrency types are process and thread, and the following posts will talk about the applications under the two concurrency types). Once the producer throws the produced data into the buffer, it can go to produce the next data. Basically do not rely on the processing speed of consumers. In fact, this mode was mainly used to deal with concurrency problems at the beginning.   ◇ There is another benefit of
   
supporting uneven  buffering.
The benefits of buffers come into play if the data is being produced at times faster and slower. When the data is produced quickly, the consumer has no time to process it, and the unprocessed data can be temporarily stored in the buffer. When the producer's manufacturing speed slows down, the consumer will slowly dispose of it.

 

【Products produced】:10 【Inventory】:+10 【Current Warehouse】:10
【Products produced】:10 【Inventory】:+20 【Current Warehouse】:20
【Products to be consumed 】 Quantity: 20 [Inventory]: 0 [Cannot perform consumption tasks temporarily!]
[Quantity of products to be consumed]: 20 [Inventory]: 10 [Cannot perform consumption tasks temporarily!]
[Quantity of products to be consumed]: 30 [Inventory] Quantity]: 10 [Cannot perform the consumption task temporarily!]
[Products produced]: 10 [Inventory]: +30 [Current storage is]: 30
[Consumed products]: 30 [Inventory]: -30 [Current Warehouse Quantity]: 0
[Quantity of Products to be Consumed]: 50 [Inventory Quantity]: 0 [Consumption tasks cannot be executed temporarily!]
[Quantity of products to be consumed]: 20 [Inventory Quantity]: 0 [Consumption tasks cannot be executed temporarily !]
【Products produced】:10 【Inventory】:+10 【Current Warehouse】:10
【Products produced】:10 【Inventory】:+20 【Current Warehouse】:20
【Already Number of products produced]: 10 [Inventory]: +30 [Current storage volume]: 30
[Products produced]: 10 [Inventory volume]:+40 [Current storage volume]: 40
[Number of products already consumed] :20 【Inventory Quantity】:-20 【Current Inventory Quantity】:20
【Products to be Consumed】:50 【Inventory Quantity】:20 【Consumption task cannot be executed temporarily!】
【Products produced】:10 【Inventory Quantity】 ]:+30 [Current storage volume is]:30
[Products produced]:10 [Inventory volume]:+40 [Current storage volume is]:40
[Production volume]:10 [Inventory volume]:+50 [The current storage capacity is]: 50
【Number of products consumed】:50 【Inventory】:-50 【Current Inventory】:0

 

[Current storage volume is]:0 [Inventory]+1
[Current storage volume is]:0 [Inventory]-1
[Current storage volume is]:0 [Inventory]-1
[Current storage volume is]:1 [Inventory] +1
[Current storage capacity is]:0 [Inventory]-1
[Current storage capacity is]:1 [Inventory]+1
[Current storage capacity is]:0 [Inventory]-1
[Current storage capacity is]:0 [ Inventory]-1
[Current Inventory]:2 [Inventory]+1
[Current Warehouse]:3 [Inventory]+1
[Current Warehouse]:4 [Inventory]+1
[Current Warehouse]: 6 【Stock】+1
【Current Warehouse】:7 【Stock】+1
【Current Warehouse】:8 【Stock】+1
【Current Warehouse】:9 【Stock】+1
【Current Warehouse 】 ]:10 [Inventory]+1
[Current inventory is]:11 [Inventory]+1
[Current storage is]:12 [Inventory]+1
[Current storage is]:13 [Inventory]+1
[Current storage ] Quantity: 1 [Stock]+1
[Current Inventory]: 14 [Stock]+1
[Current Warehouse]: 5 [Stock]+1

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326644191&siteId=291194637