Data Structures and Algorithms, abbreviated - Analysis of data structures and algorithms high performance queue behind Disruptor

Disruptor message queue


 definition

  • Memory Message Queue
  • For message passing between threads
  • Used in Apache Storm, Camel, Log4j 2 other well-known applications

"- producer consumer model" based on circular queue

  • Implement a simple "producer - consumer model."
  • For synchronous operation between producers and consumers, and did not use thread-related operations.
  • When the queue is full, the producer will wait in rotation;
  • When the queue is empty, consumers wait in rotation.
  • public class Queue {
      private Long[] data;
      private int size = 0, head = 0, tail = 0;
      public Queue(int size) {
        this.data = new Long[size];
        this.size = size;
      }
    
      public boolean add(Long element) {
        if ((tail + 1) % size == head) return false;
        data[tail] = element;
        tail = (tail + 1) % size;
        return true;
      }
    
      public Long poll() {
        if (head == tail) return null;
        long ret = data[head];
        head = (head + 1) % size;
        return ret;
      }
    }
    
    public class Producer {
      private Queue queue;
      public Producer(Queue queue) {
        this.queue = queue;
      }
    
      public void produce(Long data) throws InterruptedException {
        while (!queue.add(data)) {
          Thread.sleep(100);
        }
      }
    }
    
    public class Consumer {
      private Queue queue;
      public Consumer(Queue queue) {
        this.queue = queue;
      }
    
      public void comsume() throws InterruptedException {
        while (true) {
          Long data = queue.poll();
          if (data == null) {
            Thread.sleep(100);
          } else {
            // TODO:...消费数据的业务逻辑...
          }
        }
      }
    }

     

"- consumer model producer" lock-based concurrency

  • The above "producer - consumer model" implementation code, it is imperfect:
    • A plurality of write data producer may override each other;
    • Consumers may read more duplicate data.
  • The easiest solution is to lock the same time allowing only one thread executes add () function. Changed by the parallel serial.
  • Locking parallel into serial, time will inevitably lead to simultaneous production of a plurality of data producers, the efficiency decreases.
  • Continue to optimize the code, with CAS (compare and swap, compare and swap) operation to reduce the particle size of the lock.

"- consumer model producer" lock-based concurrency

  • The basic idea: to put it another queue and "producer - consumer model," the realization of ideas:
    • For the producer, it is added to the queue before the data, to apply for the available free storage unit , and the application is successive batches of n (n ≧ 1) memory cells.
    • When applying to the set of contiguous memory locations, the subsequent element added to the queue, the lock can not , because this thread is a set of memory locations exclusive.
    • However, the application process is the need for a storage unit locked .
    • For consumers, the processing procedure is similar with Manufacturer: go to apply it a number of continuous readable memory unit (this process is also a need to apply locked), after application to these memory cells, subsequent read operation can never be locked up.
  • Malpractice
    • A if producer to apply a set of contiguous memory locations, assuming the memory cell is denoted 3-6, the producer B followed by subscripts apply to the memory cell 7-9, and that is not completely in 3-6 before writing data, 7-9 is unreadable.
  •  

    Disruptor uses two AvailableBuffer RingBuffer and structure to achieve the above functions. 

disruptor using a data structural loops, continuous memory, and to set the object when initializing the application, the original queue head and tail nodes lock contention cas into operation, is filled using Java objects, false sharing problem solving cache line .

Guess you like

Origin www.cnblogs.com/wod-Y/p/12215309.html