Disruptor高性能緩存隊列

例子一: 一個生產者多消費者 共同消費產品

package com.h3c.disruptor02;
public class TestEvent {  

    private String line;  

    public String getLine() {  
        return line;  
    }  

    public void setLine(String line) {  
        this.line = line;  
    }  
}  
package com.h3c.disruptor02;
import com.lmax.disruptor.EventFactory;
/**
 * 事件生產工廠
 * @author gaohaicheng
 *
 */
public class TestEventFactory implements EventFactory<TestEvent>
{

    @Override
    public TestEvent newInstance() {
        return new TestEvent();
    }

}
package com.h3c.disruptor02;
import org.apache.log4j.Logger;

import com.lmax.disruptor.ExceptionHandler;

public class IntEventExceptionHandler implements ExceptionHandler {
    private static final Logger logger = Logger.getLogger(IntEventExceptionHandler.class);

    public void handleEventException(Throwable ex, long sequence, Object event) {
        logger.error("handleEventException", ex);
    }

    public void handleOnStartException(Throwable ex) {
        logger.error("handleOnStartException", ex);
    }

    public void handleOnShutdownException(Throwable ex) {
        logger.error("handleOnShutdownException", ex);
    }
}
package com.h3c.disruptor02;

import com.lmax.disruptor.WorkHandler;

public class TestEventHandler implements WorkHandler<TestEvent> {  
    @Override  
    public void onEvent(TestEvent event) throws Exception {  
        System.out.println(Thread.currentThread().getName()+" 處理了一行數據:" + event.getLine());  
    }  
}
package com.h3c.disruptor02;

import com.lmax.disruptor.EventTranslatorOneArg;
import com.lmax.disruptor.RingBuffer;

public class TestEventProducer {  

    private RingBuffer<TestEvent> ringBuffer;  

    public TestEventProducer (RingBuffer<TestEvent> ringBuffer) {  
        this.ringBuffer = ringBuffer;  
    }  

    /** 
     * 轉換器,向隊列裏放值的時候調用(隊列先設置固定長度的對象,然後通過set方法生產值) 
     */  
    private static EventTranslatorOneArg<TestEvent, String> eventTranslatorOneArg = new EventTranslatorOneArg<TestEvent, String>() {  
        @Override  
        public void translateTo(TestEvent event, long sequence, String arg0) {  
            event.setLine(arg0);  
        }  
    };  

    /** 
     * 生產者向隊列裏放入數據 
     * @param line 
     */  
    public void onData (String line) {  
        this.ringBuffer.publishEvent(eventTranslatorOneArg, line);  
    }  

    /** 
     * 處理數據 
     */  
    public void handler () {  
        long begin = System.currentTimeMillis();
        for (int i = 0; i < 20; i++) { 
            System.out.println("生產者生產了一行數據: "+"wozaizhe" + i);
            this.onData("wozaizhe" + i);  
        }  
        System.out.println("生產 者生產耗時  : "+ (System.currentTimeMillis() - begin));
    }
}
package com.h3c.disruptor02;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.lmax.disruptor.EventFactory;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.YieldingWaitStrategy;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;

public class TestEventMain {  

    public static final int BUFFER_SIZE = 2;  

    public static void main (String[] args) {  
        testDisruptor();  
    }  

    public static void testDisruptor () {  
        ExecutorService executor = Executors.newFixedThreadPool(8);  
        EventFactory<TestEvent> eventFactory = new TestEventFactory();  
        //創建disruptor,設置單生產者模式  
        Disruptor disruptor = new Disruptor(eventFactory, BUFFER_SIZE, executor, ProducerType.SINGLE,  
                new YieldingWaitStrategy ());  
        //設置消費者程序  
        disruptor.handleEventsWithWorkerPool(new TestEventHandler(), new TestEventHandler(),  
                new TestEventHandler(), new TestEventHandler());  
        //設置異常處理  
        disruptor.handleExceptionsWith(new IntEventExceptionHandler());  
        //啓動disruptor並返回RingBuffer  
        RingBuffer<TestEvent> ringBuffer = disruptor.start();  
        //創建生產者線程,並生產  
        TestEventProducer producer = new TestEventProducer(ringBuffer);  
        producer.handler();  
        disruptor.shutdown();  
        executor.shutdown();  
    }  

}  

運行結果:

生產者生產了一行數據: wozaizhe0
生產者生產了一行數據: wozaizhe1
生產者生產了一行數據: wozaizhe2
pool-1-thread-4 處理了一行數據:wozaizhe1
pool-1-thread-1 處理了一行數據:wozaizhe0
pool-1-thread-3 處理了一行數據:wozaizhe2
生產者生產了一行數據: wozaizhe3
生產者生產了一行數據: wozaizhe4
生產者生產了一行數據: wozaizhe5
pool-1-thread-4 處理了一行數據:wozaizhe4
pool-1-thread-2 處理了一行數據:wozaizhe3
生產者生產了一行數據: wozaizhe6
生產者生產了一行數據: wozaizhe7
pool-1-thread-1 處理了一行數據:wozaizhe5
pool-1-thread-3 處理了一行數據:wozaizhe6
生產者生產了一行數據: wozaizhe8
生產者生產了一行數據: wozaizhe9
pool-1-thread-4 處理了一行數據:wozaizhe7
pool-1-thread-2 處理了一行數據:wozaizhe8
pool-1-thread-1 處理了一行數據:wozaizhe9
生產者生產了一行數據: wozaizhe10
生產者生產了一行數據: wozaizhe11
生產者生產了一行數據: wozaizhe12
pool-1-thread-3 處理了一行數據:wozaizhe10
pool-1-thread-4 處理了一行數據:wozaizhe11
生產者生產了一行數據: wozaizhe13
生產者生產了一行數據: wozaizhe14
pool-1-thread-2 處理了一行數據:wozaizhe12
pool-1-thread-1 處理了一行數據:wozaizhe13
生產者生產了一行數據: wozaizhe15
生產者生產了一行數據: wozaizhe16
pool-1-thread-4 處理了一行數據:wozaizhe15
pool-1-thread-3 處理了一行數據:wozaizhe14
生產者生產了一行數據: wozaizhe17
生產者生產了一行數據: wozaizhe18
pool-1-thread-2 處理了一行數據:wozaizhe16
pool-1-thread-1 處理了一行數據:wozaizhe17
生產者生產了一行數據: wozaizhe19
pool-1-thread-4 處理了一行數據:wozaizhe18
pool-1-thread-3 處理了一行數據:wozaizhe19
生產 者生產耗時  : 125

例子二:一生產者多消費者分別消費產品

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