I. Overview
Disruptor is good at dealing with concurrent tasks, has been measured, a line thread 1s can handle 6 million orders, the performance is quite moving.
The structure of this framework is probably: data production---------and consumer
The data in the cache is actively sent to the consumer, rather than the consumer's cache of data, as in the case of a typical producer-consumer model.
Disruptor can be understood as an event-driven, efficient queue, a lightweight JMS
Disruptor Learning Website: http://ifeve.com/disruptor-getting-started
Second, the development process
1. Build event class (data Object)
2. The establishment of a production data Factory class, Eventfactory, for production data;
3. Listen for event classes (processing the events data)
4. Instantiate disruptor, configure parameters, bind events;
5. Build the core of the data Ringbuffer, the production data into the Rungbuffer.
Third, Helloword
1. Entrance
ImportJava.nio.ByteBuffer;ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors;ImportCom.lmax.disruptor.RingBuffer;ImportCom.lmax.disruptor.YieldingWaitStrategy;ImportCom.lmax.disruptor.dsl.Disruptor;ImportCom.lmax.disruptor.dsl.ProducerType; Public classLongeventmain { Public Static voidMain (string[] args)throwsException {//Create a buffer poolExecutorservice executor =Executors.newcachedthreadpool (); //Create a factoryLongeventfactory factory =Newlongeventfactory (); //Create buffersize, that is, ringbuffer size, must be 2 of the n-th square intRingbuffersize = 1024 * 1024;// /**//blockingwaitstrategy is the least effective strategy, but it consumes minimal CPU and provides more consistent performance in a variety of deployment environments waitstrategy blocking_wait = new Blo Ckingwaitstrategy (); The performance of Sleepingwaitstrategy is similar to that of Blockingwaitstrategy, but its CPU consumption is the same, but its impact on producer threads is minimal, suitable for use in similar scenarios for asynchronous logs Waitstrategy sleeping _wait = new Sleepingwaitstrategy (); The Yieldingwaitstrategy performance is the best, suitable for low latency systems. This strategy is recommended in scenarios where extreme performance is required and the number of event processing lines is less than the number of CPU logical cores; For example, the CPU turns on Hyper-Threading characteristics Waitstrategy yielding_wait = new Yieldingwaitstrategy (); */ //Create Disruptordisruptor<longevent> disruptor =NewDisruptor<longevent> (Factory, Ringbuffersize, executor, Producertype.single,Newyieldingwaitstrategy ()); //Connect Consumption event methodsDisruptor.handleeventswith (NewLongeventhandler ()); //StartDisruptor.start (); //the Disruptor event publishing process is a two-phase commit process://Publish EventsRingbuffer<longevent> Ringbuffer =Disruptor.getringbuffer (); Longeventproducer producer=NewLongeventproducer (Ringbuffer); //longeventproducerwithtranslator producer = new Longeventproducerwithtranslator (ringbuffer);Bytebuffer Bytebuffer = bytebuffer.allocate (8); for(LongL = 0; l<100; l++) {Bytebuffer.putlong (0, L); Producer.ondata (Bytebuffer); //Thread.Sleep (+);} disruptor.shutdown ();//close the disruptor, and the method will clog until all events have been processed;Executor.shutdown ();//Close the thread pool used by disruptor, and if necessary, it must be closed manually, Disruptor will not close automatically when shutdown; }}
2. Data Objects:
Public class longevent { privatelong value; Public Long GetValue () { return value; } Public void SetValue (long value) { this. Value = value;
3.Event Factory
Import com.lmax.disruptor.EventFactory; // we need to let disruptor create events for us, and we also declare a eventfactory to instantiate the event object. Publicclassimplements eventfactory { @Override public Object newinstance () { returnnew longevent ();
4. Producers
ImportJava.nio.ByteBuffer;ImportCom.lmax.disruptor.RingBuffer;/*** It is obvious that there are more details involved when publishing an event with a simple queue because the event object needs to be created in advance. * It takes at least two steps to publish an event: Get the next event slot and publish the event (use try/finnally to ensure that the event will be published when the event is published). * If we use Ringbuffer.next () to get an event slot, be sure to publish the corresponding event. * If the event cannot be published, it will cause confusion in the disruptor state. * Especially in the case of multiple event producers that can cause the event consumer to stall and thus have to restart the app to recover. */ Public classLongeventproducer {Private FinalRingbuffer<longevent>Ringbuffer; PublicLongeventproducer (ringbuffer<longevent>Ringbuffer) { This. Ringbuffer =Ringbuffer; } /*** OnData is used to publish events, once per call to publish an event * Its parameters are used to pass events to the consumer*/ Public voidonData (Bytebuffer bb) {//1. You can think of Ringbuffer as an event queue, then next is to get the following event slot Longsequence =Ringbuffer.next (); Try { //2. Use the index above to remove an empty event for padding (Gets the event object corresponding to the ordinal)Longevent event =Ringbuffer.get (sequence); //3. Get the business data to pass through the eventEvent.setvalue (Bb.getlong (0)); } finally { //4. Publishing Events//Note that the final Ringbuffer.publish method must be included in the finally to ensure that it must be called, and if the sequence of a request is not committed, it will block subsequent publishing operations or other producer. ringbuffer.publish (sequence); } } }
5. Consumers
Import Com.lmax.disruptor.EventHandler; // We also need an event consumer, which is an event handler. This event handler simply prints the data stored in the event to the terminal:Publicclassimplements eventhandler<longevent> { @Override publicvoidlong boolean Throws Exception { System.out.println (Longevent.getvalue ());} }
The architect formed a--15 of memory. Disruptor Concurrency Framework