“This is the 20th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”
Blocking queue
A queue is something like this: first in, first out
In multithreading, data sharing between threads is realized through queues, just like the previous producer-consumer pattern, data sharing between threads can be easily realized through queues. Under normal circumstances, everyone is happy to produce and take away, but life is not so good, sometimes a little accident, producers and consumers in a certain period of time, data processing speed does not match, will have to block the situation
-
The line was full, and the new guy couldn’t get in and had to wait for the first guy to get out
-
There’s nothing in the queue, so if you want to take something out but you can’t take it out, you have to wait for the production stuff to come in
List and Set are both ancestors of collections. If you look at the implementation class of a Collection, you can see that BlockingQueue is also below a Collection and is the same class as List and Set
When are blocking queues used: multi-threaded concurrent processing, thread pools
BlockingQueue API
way | An exception is thrown | Return value, no exception thrown | Block waiting for | Timeout waiting for |
---|---|---|---|---|
add | add | offer | put | Offer (data, timeout, unit of time) |
remove | remove | poll | take | Poll (waiting time, unit of time) |
Detect column head data | element | peek |
The demo implementation
The way an exception is thrown
First, we define the size of the queue as 3, which means that only three elements can be added to the queue and three elements can be removed. To test the normal situation, add three elements, remove three elements. Add and remove data, respectively
Run the program to see the results
The normal results are what we expect. After modifying the program, like adding 4 data to a queue of size 3, or adding 3 data but removing 4, run the test program and see what happens
When adding four data exception is thrown, Java. Lang. An IllegalStateException: Queue full Queue is full, when adding three data removed four data, an exception is thrown. Java util. NoSuchElementException no data
A way to return a value without throwing an exception
Don’t throw an exception if something goes wrong with adding data or removing data and when the queue is full don’t throw an exception and tell me if I added data successfully or not. Add and remove data using offer and poll, respectively
If the data is not added successfully, a fasle is returned and no exception is thrown. If the data is fetched when there are no more elements, null is returned
Waiting, blocking mode (always waiting)
It’s out of capacity and it’s adding data to it, it’s waiting when there’s no space in the queue, it’s waiting when there’s no data in the queue, it doesn’t throw an exception and it doesn’t return a value, it uses put and take to add and remove elements
View the results of the run, you will find that the console program will run and never finish waiting
Wait, block mode (timeout exit)
And third blocking way, such as a set of waiting time will not wait for, continue down, beyond the capacity of the test data left to add, queue waiting no position blocking time range, the queue has no data would be waiting for a set time after don’t wait, don’t throw an exception also has a return value, Use Offer (data, timeout, unit of time) and poll(wait time, unit of time) to add and remove elements
View the running result, the effect of the static picture can not be seen, you can run the program to see the wait time interval