TestDisruptor.java 1.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
package org.skywalking.apm.agent.core.datacarrier.performance.comparetest.disruptor;

import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.WorkHandler;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.util.DaemonThreadFactory;

/**
 * Created by wusheng on 2016/11/24.
 */
public class TestDisruptor {
    public static int totalSize = 100000000;
    public static long startTime;
    public static volatile boolean isEnd = false;

    public static void main(String[] args) throws InterruptedException {
        // The factory for the event
        DataEventFactory factory = new DataEventFactory();

        // Specify the size of the ring buffer, must be power of 2.
        int bufferSize = 1024;

        // Construct the Disruptor
        Disruptor<Data> disruptor = new Disruptor<Data>(factory, bufferSize, DaemonThreadFactory.INSTANCE);

        disruptor.handleEventsWithWorkerPool(new WorkHandler<Data>(){
            @Override
            public void onEvent(Data event) throws Exception {
                System.out.println("work1:" + event.getValue1());
            }
        }, new WorkHandler<Data>(){

            @Override
            public void onEvent(Data event) throws Exception {
                System.out.println("work2:" + event.getValue1());
            }
        });
        // Connect the handler
        disruptor.handleEventsWith(new DataEventHandler());

        // Start the Disruptor, starts all threads running
        disruptor.start();

        RingBuffer<Data> ringBuffer = disruptor.getRingBuffer();
        DataProducer producer = new DataProducer(ringBuffer);

        startTime = System.currentTimeMillis();
        for (int i = 0; i < totalSize; i++) {
            Data data = new Data();
            data.setValue1(i);
            producer.onData(data);

            Thread.sleep(1000L);
        }

        disruptor.shutdown();

        while(!TestDisruptor.isEnd){
            Thread.sleep(100L);
        }
    }
}