Disruptor個人筆記(一)

主程序代碼(來自Disruptor官網資料,略修改):

 public static void main(String[] args) throws InterruptedException { 
        // Handler線程池
        Executor executor = Executors.newCachedThreadPool();
        // 可處理對象的Factory
        LongEventFactory factory = new LongEventFactory();
        // RingBuffer的大小
        int bufferSize = 1024;
        // 處理策略 
        BusySpinWaitStrategy busy=new BusySpinWaitStrategy();
        //配置disruptor
        Disruptor<LongEvent> disruptor = new Disruptor<LongEvent>(factory, bufferSize,executor,ProducerType.SINGLE,busy);
        //在disruptor上添加Handler
        disruptor.handleEventsWith(new LongEventHandler());
        // 啓動disruptor
        disruptor.start();
        // 得到disruptor的RingBuffer 
        RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer(); 
 
        //將RingBuffer放到事件發生器裏面(RingBuffer相當於一個處理容器,以便快速處理)
        LongEventProducerWithTranslator producer = new LongEventProducerWithTranslator(ringBuffer); 
 
        //產生事件,測試代碼
        ByteBuffer bb = ByteBuffer.allocate(8);
        for (long l = 0; true; l++) { 
            bb.putLong(0, l); 
            producer.onData(bb); 
            Thread.sleep(1000); 
        } 

Disruptor的處理過程,在我看來是這樣的:


其中,各個元件的作用爲:

元數據:未加工的不可以處理的數據

Producer:將元數據加工變成可Handle的對象(加工的對象容器是Disruptor的ObjectFactory給的),並將對象發佈(Publish)給Disruptor(通過目標Disruptor的RingBuffer傳入)

Disruptor:由線程池、RingBuffer、Handler、ObjectFactory等組成,可對目標對象進行高計算密集處理,得到想要的結果


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章