Producer-consumer pattern, and a quick implementation based on BlockingQueue
What is the producer-consumer model that
Simply put, there are two roles, one that is primarily responsible for producing data and one that is primarily responsible for consuming (using) data.
So is it ok for a producer to depend directly on a consumer and then call it directly?
The answer is yes, but there are some scenarios that can't be solved in time, typically situations where the speed of the producer-consumer can't be synchronized, resulting in an overall lack of speed. The execution speed will always depend on the minimum speed of the two (assuming that the speed of the producer and the consumer is sometimes fast and sometimes slow).
What is the general solution?
We usually add a middleware that acts as a buffer queue.
The producer produces good data thrown into the buffer queue, the consumer according to their own situation in time to obtain data, processing data, as follows:
A typical implementation is Mq, such as Rocket mq, rabbit mq, etc., which is a common middleware in microservices and distributed scenarios.
Inside the service, we usually have a producer-consumer pattern as well, at which point introducing mq is clearly a bit too heavy, and we need a more efficient and concise approach.
The easiest way to do this is with a queue, or with a List, where the producer adds data from the end of the queue and the consumer takes data from the head of the queue.
But then there will be a scenario where multiple threads share data, and you need to do some locking to solve the thread synchronization problem.
Secondly when we consumers are grabbing data, we again want to have the issue of fair grabbing and non-fair grabbing of locks.
We also have to consider whether the buffer queue should be bounded, and if it is unbounded, whether there will be a problem of resource exhaustion.
And we also have to consider whether we can more elegantly balance the processing speeds of producers and consumers, for example:
If the thread state is used to control the
If the buffer queue is full, then the producer itself will hang to stop production, and when there is free space in the queue (note that it is not all emptied of data), the producer will be woken up to continue production;
If the buffer queue is empty, then the consumer hangs itself to stop consuming, and when the queue has data (note that it is not all stacked with data), the consumer is woken up to continue consuming.
These and other issues are still difficult to consider and solve if we want to develop such a buffer queue.
The toolkit provided by java8 has seen through all of this, (anti-theft link: this article was first published in /jilodream/ ) and provides a set of elegant solutionsBlockingQueue:
BlockingQueue is an interface definition, we actually use the following implementation classes:
The following methods are commonly used in BlockingQueue:
In addition, there will be a specified element (not removed) E element () / E peek (), delete the specified element boolean remove (Object o) and other methods will not be repeated here.
Let's look at a simple set of examples
The producer produces three types of gift: cell phone/computer/LV bag.
Consumer throughput
The producer-consumer are maintained using a thread pool, as detailed in the following code:
main category
1 package ; 2 3 import ; 4 import ; 5 import ; 6 import ; 7 import ; 8 import ; 9 import ; 10 import ; 11 import ; 12 13 /** 14 * @discription 15 */ 16 public class BlockQueueMain { 17 public static void main(String[] args) { 18 19 BlockingQueue<Gift> queue = new ArrayBlockingQueue<>(3); 20 Gift phone = new Gift("iphone", 8888); 21 Producer phoneProducer = new Producer(queue, "AAAA Apple Foundry.", phone); 22 23 Gift computer = new Gift("lenovo", 12888); 24 Producer computerProducer = new Producer(queue, "AAAA Lenovo Foundry", computer); 25 26 Gift bag = new Gift("LV", 28888); 27 Producer bagProducer = new Producer(queue, "AAAAA LV Foundry.", bag); 28 29 Consumer zConsumer = new Consumer(queue, "BBBB, Little Z."); 30 Consumer lConsumer = new Consumer(queue, "BBBB Little L."); 31 32 ExecutorService executorService = (); 33 (phoneProducer); 34 (computerProducer); 35 (bagProducer); 36 (zConsumer); 37 (lConsumer); 38 39 } 40 }
product category
1 package ; 2 3 import ; 4 import ; 5 import ; 6 7 import ; 8 import ; 9 import ; 10 11 /** 12 * @discription 13 */ 14 @Data 15 @NoArgsConstructor 16 public class Gift implements Delayed { 17 18 private String name; 19 20 private int price; 21 22 private final long completeTime = new Date().getTime() + 3000; 23 24 public Gift(Gift giftSample) { 25 name = (); 26 price = (); 27 } 28 29 public Gift(String name, int price) { 30 this.name = name; 31 this.price = price; 32 } 33 34 @Override 35 public long getDelay(TimeUnit unit) { 36 37 return (completeTime - new Date().getTime(), ); 38 } 39 40 @Override 41 public int compareTo(Delayed o) { 42 return 0; 43 } 44 }
Producers:
1 package ; 2 3 import .slf4j.Slf4j; 4 5 import ; 6 import ; 7 8 /** 9 * @discription 10 */ 11 @Slf4j 12 public class Producer implements Runnable { 13 14 private final BlockingQueue<Gift> queue; 15 16 private final String name; 17 18 private final Gift giftSample; 19 20 public Producer(BlockingQueue<Gift> queue, String name, Gift giftSample) { 21 this.queue = queue; 22 this.name = name; 23 this.giftSample = giftSample; 24 } 25 26 @Override 27 public void run() { 28 29 while (true) { 30 Gift newGift = new Gift(giftSample); 31 try { 32 (name + "Start of production:{}", newGift); 33 (newGift); 34 (name + "produced:{}, headroom{}", newGift, ()); 35 36 (10000); 37 } catch (InterruptedException e) { 38 ("producer {} catch a ex", name, e); 39 } 40 } 41 } 42 }
Consumers:
1 package ; 2 3 import .slf4j.Slf4j; 4 5 import ; 6 import ; 7 8 /** 9 * @discription 10 */ 11 @Slf4j 12 public class Consumer implements Runnable { 13 14 private final String name; 15 private final BlockingQueue<Gift> queue; 16 17 18 public Consumer(BlockingQueue<Gift> queue, String name) { 19 this.queue = queue; 20 this.name = name; 21 } 22 23 @Override 24 public void run() { 25 while (true) { 26 27 try { 28 Gift gift = (); 29 (name + "purchased: {} ,headroom: {}", gift, ()); 30 (3000); 31 } catch (InterruptedException e) { 32 ("Consumer {} catch a ex", name, e); 33 } 34 } 35 } 36 }
The effect after execution is as follows:
Note that the logs at a given second may be out of order, since it is a multi-threaded operation.
Interested students can analyze it for themselves
17:13:37.477 [pool-1-thread-2] WARN - AAAA Lenovo foundry begins production:Gift(name=lenovo, price=12888, completeTime=1724750022471) 17:13:37.478 [pool-1-thread-1] WARN - AAAA Apple foundry begins production:Gift(name=iphone, price=8888, completeTime=1724750022471) 17:13:37.478 [pool-1-thread-3] WARN - AAAA LVFoundry begins production:Gift(name=LV, price=28888, completeTime=1724750022473) 17:13:37.486 [pool-1-thread-1] WARN - AAAA The Apple foundry produced the:Gift(name=iphone, price=8888, completeTime=1724750022471), remaining space1 17:13:37.486 [pool-1-thread-4] WARN - BBBB fewZpurchased: Gift(name=iphone, price=8888, completeTime=1724750022471) ,headroom: 2 17:13:37.486 [pool-1-thread-5] WARN - BBBB fewLpurchased: Gift(name=lenovo, price=12888, completeTime=1724750022471) ,headroom: 2 17:13:37.486 [pool-1-thread-2] WARN - AAAA The Lenovo foundry produced the:Gift(name=lenovo, price=12888, completeTime=1724750022471), remaining space 2 17:13:37.486 [pool-1-thread-3] WARN - AAAA LVThe foundry produced the:Gift(name=LV, price=28888, completeTime=1724750022473), remaining space 2 17:13:40.491 [pool-1-thread-4] WARN - BBBB fewZpurchased: Gift(name=LV, price=28888, completeTime=1724750022473) ,headroom: 3 17:13:47.491 [pool-1-thread-3] WARN - AAAA LVFoundry begins production:Gift(name=LV, price=28888, completeTime=1724750032491) 17:13:47.491 [pool-1-thread-1] WARN - AAAA Apple foundry begins production:Gift(name=iphone, price=8888, completeTime=1724750032491) 17:13:47.491 [pool-1-thread-2] WARN - AAAA Lenovo foundry begins production:Gift(name=lenovo, price=12888, completeTime=1724750032491) 17:13:47.491 [pool-1-thread-2] WARN - AAAA The Lenovo foundry produced the:Gift(name=lenovo, price=12888, completeTime=1724750032491), remaining space1 17:13:47.491 [pool-1-thread-3] WARN - AAAA LVThe foundry produced the:Gift(name=LV, price=28888, completeTime=1724750032491), remaining space 2 17:13:47.491 [pool-1-thread-4] WARN - BBBB fewZpurchased: Gift(name=iphone, price=8888, completeTime=1724750032491) ,headroom: 2 17:13:47.491 [pool-1-thread-1] WARN - AAAA The Apple foundry produced the:Gift(name=iphone, price=8888, completeTime=1724750032491), remaining space 2 17:13:47.491 [pool-1-thread-5] WARN - BBBB fewLpurchased: Gift(name=LV, price=28888, completeTime=1724750032491) ,headroom: 2
Next, we will briefly describe the internal logic and usage scenarios of several BlockingQueue implementations.
1、ArrayBlockingQueue
Internally, buffers are implemented using arrays, and (anti-theft link: this article was first published from /jilodream/ ) bounded queues;
Using ReentrantLock locks for thread safety
It can meet most of the business scenarios.
2、LinkedBlockingQueue
Internally, a single linked table is used to implement the buffer, which can be either an unbounded queue or a bounded queue;
Use read and write locks for thread safety.
Applicable to the queue elasticity is relatively large, concurrency requirements of high scenarios.
3、SynchronousQueue
synchronized queue
Many people claim that if they want tasks to execute quickly, they can use this queue. It's easy for others to misunderstand, so why don't we take a practical example
Switch the blockingqueue in the main class to a SynchronousQueue.
BlockingQueue<Gift> queue = new SynchronousQueue<>();
The input is below, and below is the output for the first 13 seconds is, with my explanation in red:
15:35:30.906 [pool-1-thread-3] WARN - AAAA LVFoundry begins production:Gift(name=LV, price=28888, completeTime=1724744135902) 15:35:30.915 [pool-1-thread-3] WARN - AAAA LVThe foundry produced the:Gift(name=LV, price=28888, completeTime=1724744135902), remaining space 0 15:35:30.907 [pool-1-thread-2] WARN - AAAA Lenovo foundry begins production:Gift(name=lenovo, price=12888, completeTime=1724744135901) 15:35:30.915 [pool-1-thread-5] WARN - BBBB fewLpurchased: Gift(name=LV, price=28888, completeTime=1724744135902) ,headroom: 0 15:35:30.906 [pool-1-thread-1] WARN - AAAA Apple foundry begins production:Gift(name=iphone, price=8888, completeTime=1724744135901) 15:35:30.915 [pool-1-thread-2] WARN - AAAA The Lenovo foundry produced the:Gift(name=lenovo, price=12888, completeTime=1724744135901), remaining space 0 15:35:30.915 [pool-1-thread-4] WARN - BBBB fewZpurchased: Gift(name=lenovo, price=12888, completeTime=1724744135901) ,headroom: 0 After the request, the factory produced three separate products: cell phones, computers, and LVs
After that, both the LV and the computer are consumed, and the iPhone is still pending consumption.
15:35:33.916 [pool-1-thread-5] WARN - BBBB fewLpurchased: Gift(name=iphone, price=8888, completeTime=1724744135901) ,headroom: 0 15:35:33.916 [pool-1-thread-1] WARN - AAAA The Apple foundry produced the:Gift(name=iphone, price=8888, completeTime=1724744135901), remaining space 0 Consumer sleeps for 3 seconds after consuming the cell phone
It's only at this point that the phone's producer starts printing the log, signaling the end of the put method's blocking state
Note the last point in time 15:35:33.916
After the consumer's second consumption, the iPhone's manufacturer immediately printed the log, so the iPhone's manufacturer had previously been in a blocking state while placing the data.
That is, if the consumer doesn't fetch, then the producer stays in the rendered state. This is exactly the kind of scenario that can be used if it is a fast consumption scenario, rather than this scenario allows you to execute your task quickly. Of course we can actually tell by the name.
Is this scenario realistic?
There are also some, such as the waiter to make change, will usually put the money in the hand, waiting for you to take the money before doing the next action. Like this need to synchronize the handover, fast response scene, then you can consider SynchronousQueue.
Of course, in real-world scenarios, the waiter can't wait all the time, the consumer may not take the money all the time (the timeout is set), and the consumer doesn't collect the money (the blocking is broken by InterruptedException).
4、PriorityBlockingQueue
Prioritized blocking queues, the idea is really simple, is that consumers no longer get data first-in-first-out, but are allowed to take products with high priority after sorting them according to priority. The queue is internally maintained by a minimal heap, and we create the queue when the
To specify the comparison algorithm. As follows, we use the way to get the products according to the price from high to low, while adjusting the producer speed > consumer speed (adjust the producer sleep to 1000ms can be):
BlockingQueue<Gift> queue = new PriorityBlockingQueue<>(3, (o1, o2) -> () - ());
The output is as follows, we can see that the consumer gets only the highest value product in the current queue:
18:39:01.040 [pool-1-thread-1] WARN - AAAA Apple foundry begins production:Gift(name=iphone, price=8888, completeTime=1724755146036) 18:39:01.040 [pool-1-thread-2] WARN - AAAA Lenovo foundry begins production:Gift(name=lenovo, price=12888, completeTime=1724755146036) 18:39:01.040 [pool-1-thread-3] WARN - AAAA LVFoundry begins production:Gift(name=LV, price=28888, completeTime=1724755146036) 18:39:01.045 [pool-1-thread-3] WARN - AAAA LVThe foundry produced the:Gift(name=LV, price=28888, completeTime=1724755146036), remaining space 2147483647 18:39:01.045 [pool-1-thread-4] WARN - BBBB fewZpurchased: Gift(name=LV, price=28888, completeTime=1724755146036) ,headroom: 2147483647 18:39:01.045 [pool-1-thread-2] WARN - AAAA The Lenovo foundry produced the:Gift(name=lenovo, price=12888, completeTime=1724755146036), remaining space 2147483647 18:39:01.045 [pool-1-thread-5] WARN - BBBB fewLpurchased: Gift(name=lenovo, price=12888, completeTime=1724755146036) ,headroom: 2147483647 18:39:01.045 [pool-1-thread-1] WARN - AAAA The Apple foundry produced the:Gift(name=iphone, price=8888, completeTime=1724755146036), remaining space 2147483647 18:39:02.059 [pool-1-thread-3] WARN - AAAA LVFoundry begins production:Gift(name=LV, price=28888, completeTime=1724755147059) ... Omit a portion of the log .... 18:39:03.069 [pool-1-thread-3] WARN - AAAA LVThe foundry produced the:Gift(name=LV, price=28888, completeTime=1724755148069), remaining space 2147483647 18:39:03.069 [pool-1-thread-2] WARN - AAAA The Lenovo foundry produced the:Gift(name=lenovo, price=12888, completeTime=1724755148069), remaining space 2147483647 18:39:04.052 [pool-1-thread-4] WARN - BBBB fewZpurchased: Gift(name=LV, price=28888, completeTime=1724755148069) ,headroom: 2147483647 18:39:04.052 [pool-1-thread-5] WARN - BBBB fewLpurchased: Gift(name=LV, price=28888, completeTime=1724755147059) ,headroom: 2147483647
5、DelayQueue
This queue is also very interesting, it needs to have a product to implement a delay interface, the interface in real time to return to how long the product is available.
We switch to DelayQueue as follows.
The effect is as follows:
Consumers can't get it immediately even though they have triggered the fetch, (anti-theft link: this article was first published in /jilodream/ ) but need to get it after delay() returns a non-positive number. This way the producer can produce something that has an internal concept of time, acting as a timer.
An example:
We renovated a house and due to hazardous materials in the renovation it will take us 6 months before we can move in,...
The commonly used approaches are as follows:
1, add a timer, triggered by the timer, but obviously there should be external dependencies on other objects.
2, repeatedly get, get after check time, if not to time, re-throw back to the queue, but this is obviously too complex, the system will go up the frequency of calls.
3, the consumer to get the product, determine whether the time is desirable, if not desirable to wait until the product can be taken to the end. Note that at this time the consumer thread will block to the head of the queue products, and will not cross it to try to get the subsequent products. (DelayQueue choose this strategy)
We'll start by modifying the buffer queue to be DelayQueue.
BlockingQueue<Gift> queue = new DelayQueue<>();
The time period of the producer is changed to 2s and the time period of the consumer is changed to 4s .Assuming the expiration time of the product is 5s,Paying attention to the timing of these
The code of the Gift class needs to be revamped to make it easier to locate and analyze:
1 package ; 2 3 import ; 4 import ; 5 import .slf4j.Slf4j; 6 7 import ; 8 import ; 9 import ; 10 11 /** 12 * @discription 13 */ 14 @Data 15 @NoArgsConstructor 16 @Slf4j 17 public class Gift implements Delayed { 18 19 private String name; 20 21 private int price; 22 23 private final long completeTime = new Date().getTime() + 5000; 24 25 public Gift(Gift giftSample) { 26 name = () + "_" + completeTime; 27 price = (); 28 } 29 30 public Gift(String name, int price) { 31 this.name = name; 32 this.price = price; 33 } 34 35 private void printLog() { 36 (() + " !!!!"+ "_" + name); 37 } 38 39 @Override 40 public long getDelay(TimeUnit unit) { 41 printLog(); 42 return (completeTime - new Date().getTime(), ); 43 } 44 45 @Override 46 public int compareTo(Delayed o) { 47 return 0; 48 } 49 }
The effect is probably something like this:
Is it not quite what we envisioned, and it's straightforward to say what happened, noting that different colors of analysis correspond to different colors of logs:
First is thread 5 The consumer grabs the lock, then waits directly until the product expires, note that thread 5 is not checking in the cycle we agreed upon, then confirms the time a second time, and then consumes it (24 seconds --->29 seconds)
At this point, thread 4 grabs the next lock.,Note that the next gift is the latest production, not the earliest!,Then wait for the product expiration time, the second confirmation and consumption (28 seconds ---->33 seconds) Note that in the second confirmation of the time of the time of the time of the two threads have confirmed, but only one thread will actually consume the success of this does not affect the use of the effect (guess here is to improve performance, and did not completely, did not use a very heavy lock).
The same logic follows.
Connected to the target VM, address: '127.0.0.1:54328', transport: 'socket' 19:52:24.292 [pool-1-thread-3] WARN - AAAA LVFoundry begins production:Gift(name=LV_1724759549288, price=28888, completeTime=1724759549288) 19:52:24.292 [pool-1-thread-2] WARN - AAAA Lenovo foundry begins production:Gift(name=lenovo_1724759549289, price=12888, completeTime=1724759549289) 19:52:24.292 [pool-1-thread-1] WARN - AAAA Apple foundry begins production:Gift(name=iphone_1724759549288, price=8888, completeTime=1724759549288) 19:52:24.300 [pool-1-thread-5] WARN - Thread[pool-1-thread-5,5,main] !!!!_LV_1724759549288 19:52:24.300 [pool-1-thread-3] WARN - AAAA LVThe foundry produced the:Gift(name=LV_1724759549288, price=28888, completeTime=1724759549288), remaining space 2147483647 19:52:24.300 [pool-1-thread-2] WARN - AAAA The Lenovo foundry produced the:Gift(name=lenovo_1724759549289, price=12888, completeTime=1724759549289), remaining space 2147483647 19:52:24.302 [pool-1-thread-1] WARN - AAAA The Apple foundry produced the:Gift(name=iphone_1724759549288, price=8888, completeTime=1724759549288), remaining space 2147483647 19:52:26.311 [pool-1-thread-2] WARN - AAAA Lenovo foundry begins production:Gift(name=lenovo_1724759551311, price=12888, completeTime=1724759551311) 19:52:26.311 [pool-1-thread-1] WARN - AAAA Apple foundry begins production:Gift(name=iphone_1724759551311, price=8888, completeTime=1724759551311) 19:52:26.311 [pool-1-thread-3] WARN - AAAA LVFoundry begins production:Gift(name=LV_1724759551311, price=28888, completeTime=1724759551311) 19:52:26.311 [pool-1-thread-2] WARN - AAAA The Lenovo foundry produced the:Gift(name=lenovo_1724759551311, price=12888, completeTime=1724759551311), remaining space 2147483647 19:52:26.311 [pool-1-thread-1] WARN - AAAA The Apple foundry produced the:Gift(name=iphone_1724759551311, price=8888, completeTime=1724759551311), remaining space 2147483647 19:52:26.311 [pool-1-thread-3] WARN - AAAA LVThe foundry produced the:Gift(name=LV_1724759551311, price=28888, completeTime=1724759551311), remaining space 2147483647 19:52:28.321 [pool-1-thread-3] WARN - AAAA LVFoundry begins production:Gift(name=LV_1724759553321, price=28888, completeTime=1724759553321) 19:52:28.321 [pool-1-thread-2] WARN - AAAA Lenovo foundry begins production:Gift(name=lenovo_1724759553321, price=12888, completeTime=1724759553321) 19:52:28.321 [pool-1-thread-1] WARN - AAAA Apple foundry begins production:Gift(name=iphone_1724759553321, price=8888, completeTime=1724759553321) 19:52:28.321 [pool-1-thread-3] WARN - AAAA LVThe foundry produced the:Gift(name=LV_1724759553321, price=28888, completeTime=1724759553321), remaining space 2147483647 19:52:28.321 [pool-1-thread-2] WARN - AAAA The Lenovo foundry produced the:Gift(name=lenovo_1724759553321, price=12888, completeTime=1724759553321), remaining space 2147483647 19:52:28.321 [pool-1-thread-1] WARN - AAAA The Apple foundry produced the:Gift(name=iphone_1724759553321, price=8888, completeTime=1724759553321), remaining space 2147483647 19:52:29.300 [pool-1-thread-5] WARN - Thread[pool-1-thread-5,5,main] !!!!_LV_1724759549288 19:52:29.300 [pool-1-thread-4] WARN - Thread[pool-1-thread-4,5,main] !!!!_iphone_1724759553321 19:52:29.300 [pool-1-thread-5] WARN - BBBB fewLpurchased: Gift(name=LV_1724759549288, price=28888, completeTime=1724759549288) ,headroom: 2147483647 19:52:30.326 [pool-1-thread-3] WARN - AAAA LVFoundry begins production:Gift(name=LV_1724759555326, price=28888, completeTime=1724759555326) 19:52:30.326 [pool-1-thread-1] WARN - AAAA Apple foundry begins production:Gift(name=iphone_1724759555326, price=8888, completeTime=1724759555326) 19:52:30.326 [pool-1-thread-2] WARN - AAAA Lenovo foundry begins production:Gift(name=lenovo_1724759555326, price=12888, completeTime=1724759555326) 19:52:30.326 [pool-1-thread-1] WARN - AAAA The Apple foundry produced the:Gift(name=iphone_1724759555326, price=8888, completeTime=1724759555326), remaining space 2147483647 19:52:30.326 [pool-1-thread-2] WARN - AAAA The Lenovo foundry produced the:Gift(name=lenovo_1724759555326, price=12888, completeTime=1724759555326), remaining space 2147483647 19:52:30.326 [pool-1-thread-3] WARN - AAAA LVThe foundry produced the:Gift(name=LV_1724759555326, price=28888, completeTime=1724759555326), remaining space 2147483647 19:52:32.334 [pool-1-thread-3] WARN - AAAA LVFoundry begins production:Gift(name=LV_1724759557334, price=28888, completeTime=1724759557334) 19:52:32.334 [pool-1-thread-2] WARN - AAAA Lenovo foundry begins production:Gift(name=lenovo_1724759557334, price=12888, completeTime=1724759557334) 19:52:32.334 [pool-1-thread-1] WARN - AAAA Apple foundry begins production:Gift(name=iphone_1724759557334, price=8888, completeTime=1724759557334) 19:52:32.334 [pool-1-thread-3] WARN - AAAA LVThe foundry produced the:Gift(name=LV_1724759557334, price=28888, completeTime=1724759557334), remaining space 2147483647 19:52:32.334 [pool-1-thread-1] WARN - AAAA The Apple foundry produced the:Gift(name=iphone_1724759557334, price=8888, completeTime=1724759557334), remaining space 2147483647 19:52:32.334 [pool-1-thread-2] WARN - AAAA The Lenovo foundry produced the:Gift(name=lenovo_1724759557334, price=12888, completeTime=1724759557334), remaining space 2147483647 19:52:33.301 [pool-1-thread-5] WARN - Thread[pool-1-thread-5,5,main] !!!!_iphone_1724759553321 19:52:33.331 [pool-1-thread-4] WARN - Thread[pool-1-thread-4,5,main] !!!!_iphone_1724759553321 19:52:33.331 [pool-1-thread-5] WARN - Thread[pool-1-thread-5,5,main] !!!!_iphone_1724759557334 19:52:33.331 [pool-1-thread-4] WARN - BBBB fewZpurchased: Gift(name=iphone_1724759553321, price=8888, completeTime=1724759553321) ,headroom: 2147483647 19:52:34.346 [pool-1-thread-2] WARN - AAAA Lenovo foundry begins production:Gift(name=lenovo_1724759559346, price=12888, completeTime=1724759559346) 19:52:34.346 [pool-1-thread-3] WARN - AAAA LVFoundry begins production:Gift(name=LV_1724759559346, price=28888, completeTime=1724759559346) 19:52:34.346 [pool-1-thread-1] WARN - AAAA Apple foundry begins production:Gift(name=iphone_1724759559346, price=8888, completeTime=1724759559346) 19:52:34.346 [pool-1-thread-2] WARN - AAAA The Lenovo foundry produced the:Gift(name=lenovo_1724759559346, price=12888, completeTime=1724759559346), remaining space 2147483647 19:52:34.346 [pool-1-thread-3] WARN - AAAA LVThe foundry produced the:Gift(name=LV_1724759559346, price=28888, completeTime=1724759559346), remaining space 2147483647 19:52:34.346 [pool-1-thread-1] WARN - AAAA The Apple foundry produced the:Gift(name=iphone_1724759559346, price=8888, completeTime=1724759559346), remaining space 2147483647 19:52:36.361 [pool-1-thread-3] WARN - AAAA LVFoundry begins production:Gift(name=LV_1724759561361, price=28888, completeTime=1724759561361) 19:52:36.361 [pool-1-thread-2] WARN - AAAA Lenovo foundry begins production:Gift(name=lenovo_1724759561361, price=12888, completeTime=1724759561361) 19:52:36.361 [pool-1-thread-1] WARN - AAAA Apple foundry begins production:Gift(name=iphone_1724759561361, price=8888, completeTime=1724759561361) 19:52:36.361 [pool-1-thread-3] WARN - AAAA LVThe foundry produced the:Gift(name=LV_1724759561361, price=28888, completeTime=1724759561361), remaining space 2147483647 19:52:36.361 [pool-1-thread-2] WARN - AAAA The Lenovo foundry produced the:Gift(name=lenovo_1724759561361, price=12888, completeTime=1724759561361), remaining space 2147483647 19:52:36.361 [pool-1-thread-1] WARN - AAAA The Apple foundry produced the:Gift(name=iphone_1724759561361, price=8888, completeTime=1724759561361), headroom2147483647 Disconnected from the target VM, address:'127.0.0.1:54328', transport: 'socket'(anti-theft connection:This article first appeared on/jilodream/ ) 19:52:37.339 [pool-1-thread-5] WARN - Thread[pool-1-thread-5,5,main] !!!!_iphone_1724759557334 19:52:37.340 [pool-1-thread-4] WARN - Thread[pool-1-thread-4,5,main] !!!!_iphone_1724759561361 19:52:37.339 [pool-1-thread-5] WARN - BBBB fewLpurchased: Gift(name=iphone_1724759557334, price=8888, c
We can draw two important conclusions from the above:
The DelayQueue is not consumed sequentially, but rather the latest data is fetched to try to get it, (even though there are already products available in the queue at this point)
DelayQueue wakes up the consumer according to the amount of time it takes for the product to reach an available state before it wakes up
So when using DelayQueue, we have to be careful that the production of goods is not too intense and that the production rate is not greater than the consumption rate, otherwise the product will probably not be consumed (and of course we can take advantage of this rule by eliminating old products)
The product's DELAY time should not be too long or it may cause the consumer to stay hung up.