Disruptor creation Process

Source: Internet
Author: User

1 disruptor disruptor = new Disruptor<valueevent> (valueevent.event_factory, ringbuffersize, exec,
Producertype.multi, Waitstrategy);

     Public Disruptor (final eventfactory<t> eventfactory,                     finalint  Ringbuffersize,                     final  Executor Executor,                     final  Producertype Producertype,                     final  waitstrategy waitstrategy)    {        this  (Ringbuffer.create (Producertype, Eventfactory, Ringbuffersize, waitstrategy),             executor);    }
  
     Public Static<E> ringbuffer<e>Create (Producertype producertype, Eventfactory<E>Factory,intbuffersize, Waitstrategy waitstrategy) {        Switch(producertype) { CaseSingle :returnCreatesingleproducer (Factory, BufferSize, Waitstrategy);  CaseMULTI:returnCreatemultiproducer (Factory, BufferSize, Waitstrategy); default:            Throw Newillegalstateexception (producertype.tostring ()); }    }

  

Createmultiproducer:

     Public Static<E> ringbuffer<e> Createmultiproducer (eventfactory<e>Factory,intbuffersize, Waitstrategy waitstrategy) {Multiproducersequencer Sequencer=Newmultiproducersequencer (buffersize, waitstrategy);//Sequencer
Public final class Multiproducersequencer extends Abstractsequencer
Public abstract class Abstractsequencer implements Sequencerreturn NewRingbuffer<e>(factory, sequencer); }
  
Ringbuffer (eventfactory<e>eventfactory, Sequencer Sequencer) {         This. Sequencer =Sequencer;  This. buffersize =sequencer.getbuffersize (); if(BufferSize < 1)        {            Throw NewIllegalArgumentException ("buffersize must not being less than 1"); }        if(Integer.bitcount (buffersize)! = 1)        {            Throw NewIllegalArgumentException ("buffersize must be a power of 2"); }         This. Indexmask = bufferSize-1;  This. Entries =Newobject[sequencer.getbuffersize ()];    Fill (eventfactory); }
    
    Private void Fill (eventfactory<e> eventfactory)    {        for (int i = 0; i < Entries.length; i++)        {            = eventfactory.newinstance ();        }    }

2 Disruptor.handleeventswith (eventhandlers);

  

     Public Eventhandlergroup<t> Handleeventswith (final eventhandler<t> ...handlers)    {         return createeventprocessors (new sequence[0], handlers);    }
Eventhandlergroup<t> Createeventprocessors (Finalsequence[] barriersequences,FinalEventhandler<t>[] eventhandlers)        {checknotstarted (); Finalsequence[] Processorsequences =NewSequence[eventhandlers.length]; FinalSequencebarrier barrier =Ringbuffer.newbarrier (barriersequences);//
Processingsequencebarrier Processingsequencebarrier implements Sequencebarrier for(inti = 0, eventhandlerslength = eventhandlers.length; i < eventhandlerslength; i++) { FinalEventhandler<t> EventHandler =Eventhandlers[i]; FinalBatcheventprocessor<t> Batcheventprocessor =NewBatcheventprocessor<t>(Ringbuffer, Barrier, EventHandler); if(Exceptionhandler! =NULL) {Batcheventprocessor.setexceptionhandler (Exceptionhandler); } consumerrepository.add (Batcheventprocessor, EventHandler, barrier); Processorsequences[i]=batcheventprocessor.getsequence (); } if(Processorsequences.length > 0) {consumerrepository.unmarkeventprocessorsasendofchain (barriersequences); } return NewEventhandlergroup<t> ( This, Consumerrepository, processorsequences); }
Consumerrepository is associated with Batcheventprocessor EventHandler Sequencebarrier.
Eventhandlergroup is associated with disruptor consumerrepository Sequence.


  

3 Disruptor.start ();

     PublicRingbuffer<t>start () {sequence[] gatingsequences= Consumerrepository.getlastsequenceinchain (true);        Ringbuffer.addgatingsequences (gatingsequences);        Checkonlystartedonce ();  for(Consumerinfo consumerinfo:consumerrepository) {consumerinfo.start (executor);//Custom thread pool execution Eventpro Cessorinfo}returnRingbuffer; }

Class Eventprocessorinfo<t> implements Consumerinfo
@Override
public void Start (final Executor Executor)
{
Executor.execute (eventprocessor);//Batcheventprocessor
}

Batcheventprocessor Run method
@Override
public void Run ()
{
if (!running.compareandset (False, True))
{
throw new IllegalStateException ("Thread is already running");
}
Sequencebarrier.clearalert ();

Notifystart ();

T event = null;
Concurrent sequence class used for tracking the progress of
* The ring buffer and event processors. Support a number
* of concurrent operations including CAS and order writes
Long nextsequence = Sequence.get () + 1l;//use CAs to get the next serial number
Try
{
while (true)
{
Try
{
Final Long availablesequence = Sequencebarrier.waitfor (nextsequence);//wait for the policy to get the available serial number

if (NextSequence > Availablesequence)//
{
Thread.yield ();
}

while (NextSequence <= availablesequence)
{
event = Dataprovider.get (nextsequence);//DataproviderIt's ringbuffer.
Eventhandler.onevent (event, nextsequence, nextsequence = = availablesequence);//Touch Custom Event
nextsequence++;
}

Sequence.set (availablesequence);
}
catch (Final timeoutexception e)
{
Notifytimeout (Sequence.get ());
}
catch (final alertexception ex)
{
if (!running.get ())
{
Break
}
}
catch (final Throwable ex)
{
Exceptionhandler.handleeventexception (ex, NextSequence, event);
Sequence.set (nextsequence);
nextsequence++;
}
}
}
Finally
{
Notifyshutdown ();
Running.set (FALSE);
}
}

4 Introduction to final long availablesequence = Sequencebarrier.waitfor (nextsequence);//Waiting for available serial number

6 Wait Strategies

  

Disruptor creation Process

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.