We have learned from the previous article what isRing BufferAnd how special it is. Unfortunately, I have not told you how to use it.DisruptorDirectionRing BufferWrite Data andRing Buffer.
ConsumerBarrier and consumerHere I will introduce it in turn, because reading data is easier to understand than writing data. Assume that data has been written
Ring BufferHow
Ring BufferWhat about reading this data?
(Well, I began to regret it.Paint/Gimp. Although this is a good excuse to buy a drawing board, if I continue to write it...UMLThe authorities in the world are also cursing my name .)
Consumer (Consumer) IsRing BufferThe thread that reads data.ConsumerBarrierObject-this object consistsRingBufferCreate and represent the consumer andRingBuffer. Just likeRing BufferObviously, a sequence number is required to locate the next available node. The consumer also needs to know the sequence number it will process-each consumer needs to find the sequence number to be accessed by the next node. In the above example, the consumer has finished processingRing BufferSerial number8Previously included8), The next serial number to be accessed is9.
Consumers can callConsumerBarrierObjectWaitFor ()Method to pass the next sequence number required by it.
1 |
final long availableSeq = consumerBarrier.waitFor(nextSequence); |
ConsumerBarrierReturnRingBuffer-In the preceding example12.ConsumerBarrierThere isWaitStrategyMethod to Determine how it waits for the serial number. I will not describe its details now. The comments of the Code have summarized eachWaitStrategy.
What should we do next?
Next, the consumer will stay in the same place and wait for more data to be written.Ring Buffer. In addition, once the data is written, the consumer will receive a notification-node9,10,11And12Written. Current serial number12Now, consumers can makeConsumerBarrierGet the data in these node numbers.
After obtaining the data, the consumer (Consumer) Will update your identity (Cursor).
You should have known how this helps smooth the latency peak-I used to ask each node one by one, "can I take the next data? Is it okay now? What about now ?", Consumer (ConsumerNow, you just need to simply say, "Please tell me when you get a number that is bigger than me ", the Return Value of the function indicates how many new nodes can read data. Because these new nodes have indeed written dataRing BufferItself), and the consumer's only operation on these nodes is read rather than write, so there is no need to lock the access. This is too good. Not only can the code be implemented more securely and simply, but it does not need to be locked to make the speed faster.
Another benefit is that you can use multiple consumers (Consumer)Read the sameRingBufferNo locks are required, or different queues are required to coordinate different threads (consumers ). In this way, you canDisruptorTo achieve real concurrent data processing.
BatchConsumerCode is an example of a consumer. If you implementBatchHandler,You can useBatchConsumerTo complete the complex work I mentioned above. It can easily deal with nodes that need batch processing, such9-12Node) instead of reading each node separately.
Update: Note:Disruptor 2.0The version uses a different name than this document. If you are confused about the class name, please read myChange Summary.
Http://ifeve.com/dissecting_the_disruptor_how_doi_read_from_the_ring_buffer/.