I have an idea of how this class works, but I want to understand in what situation its implementation will bring me benefits, I can’t even think of it, help please understand.
2 answers
BlockingQueue
is a Queue
with the following additions:
- The blocking operation
take()
- takes the next element, if the queue is empty - blocks execution until the element appears (until the moment when it ceases to be empty) - The blocking
put(E e)
operationput(E e)
- puts an item in the queue, if the queue is full - blocks execution until the place in the queue is cleared and the new item is successfully placed in it.
Designed to implement the interaction "supplier - consumer". Implementations of methods are thread-safe (synchronized), so you can write to it and read from it in several threads. Quote from JavaDoc:
Note that a BlockingQueue can be used with multiple producers and multiple consumers .
Example 1: Let there be a store with 3 entrances and 2 cash registers. Buyers rod through 3 entrances and exit through 2 cash registers. The queue at the box office total. It is advisable to store a queue of buyers in the BlockingQueue
.
Example 2: There is a conveyor that produces parts. For the production of the necessary blanks. From several machines of the previous stage of production, blanks come in, robots take them and make parts of them. In the next step, these parts are used to produce components by other robots. It is advisable to store the queue of incoming blanks and the queue of outgoing parts (for the next stage) in the BlockingQueue
.
- Thanks for the answer and examples. - Dasha
BlockingQueue - as you can understand from the documentation is used to perform an operation on data in a different place or time, not where it was received.
The most common example is message processing (data refresh). Data is received (events) in one stream (there may be several of them) and are recorded in the BlockingQueue, at the same time another then (it should be one) waits for elements in this queue (method take) and as soon as it receives data it starts processing. This avoids the situation when several streams try to write the same data.
- This is very interesting, but to avoid recording the same data you can simply synchronize it in different ways, but here I understand that we start threads in portions, but then how will this differ from the same fixed pool, for example Executors.newFixedThreadPool (10); ? I still do not fully understand the meaning of the queue, just a class that has another hierarchy for grouping threads with the ability to run them in portions? Another abstract cell for performing large operations separated by multiple threads. - Dasha
- @Dasha it is about recording the queue, the sequence of data that will be processed (for example, written to disk, etc.) sequentially in another stream. In this very "other stream" they need to be transferred somehow. You can transfer each object for processing without creating a queue, but then you will not be able to “add to the queue” new incoming data. In other words, if you solve some similar practical problem by other means, you end up writing the same BlockingQueue - Vladimir Bershov