disruptor

Disruptor它是一个开源的并发框架,并获得2011 Duke's 程序框架创新奖,能够在无锁的情况下实现网络的Queue并发操作。

来一个他的例子吧.绝对是hello worl级别的.

只有两个类,一个是执行类,一个是自己定义的类.
自己需要定义一个event类,

Java代码 复制代码 收藏代码
  1. package com.trevorbernard.disruptor.examples; 
  2.  
  3. import com.lmax.disruptor.EventFactory; 
  4.  
  5. /**
  6. * WARNING: This is a mutable object which will be recycled by the RingBuffer. You must take a copy of data it holds
  7. * before the framework recycles it.
  8. */ 
  9. public final class ValueEvent { 
  10.     private String value; 
  11.  
  12.     public String getValue() { 
  13.         return value; 
  14.     } 
  15.  
  16.     public void setValue(String value) { 
  17.         this.value = value; 
  18.     } 
  19.  
  20.     public final static EventFactory<ValueEvent> EVENT_FACTORY = new EventFactory<ValueEvent>() { 
  21.         public ValueEvent newInstance() { 
  22.             return new ValueEvent(); 
  23.         } 
  24.     }; 
package com.trevorbernard.disruptor.examples;

import com.lmax.disruptor.EventFactory;

/**
 * WARNING: This is a mutable object which will be recycled by the RingBuffer. You must take a copy of data it holds
 * before the framework recycles it.
 */
public final class ValueEvent {
    private String value;

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public final static EventFactory<ValueEvent> EVENT_FACTORY = new EventFactory<ValueEvent>() {
        public ValueEvent newInstance() {
            return new ValueEvent();
        }
    };
}


这里要用到一个factory类,作用就是生成定义的那个类.

执行类
Java代码 复制代码 收藏代码
  1. package com.trevorbernard.disruptor.examples; 
  2.  
  3. import java.util.UUID; 
  4. import java.util.concurrent.ExecutorService; 
  5. import java.util.concurrent.Executors; 
  6.  
  7. import com.lmax.disruptor.EventHandler; 
  8. import com.lmax.disruptor.RingBuffer; 
  9. import com.lmax.disruptor.dsl.Disruptor; 
  10.  
  11. public class Simple { 
  12.     @SuppressWarnings("unchecked"
  13.     public static void main(String[] args) { 
  14.         ExecutorService exec = Executors.newCachedThreadPool(); 
  15.         // Preallocate RingBuffer with 1024 ValueEvents 
  16.         Disruptor<ValueEvent> disruptor = new Disruptor<ValueEvent>(ValueEvent.EVENT_FACTORY, 1024, exec); 
  17.         final EventHandler<ValueEvent> handler = new EventHandler<ValueEvent>() { 
  18.             // event will eventually be recycled by the Disruptor after it wraps 
  19.             public void onEvent(final ValueEvent event, final long sequence, final boolean endOfBatch) throws Exception { 
  20.                 System.out.println("Sequence: " + sequence+"   ValueEvent: " + event.getValue()); 
  21.             } 
  22.         }; 
  23.  
  24.         disruptor.handleEventsWith(handler); 
  25.         RingBuffer<ValueEvent> ringBuffer = disruptor.start(); 
  26.         for (long i = 10; i < 15; i++) { 
  27.             String uuid =String.valueOf(i)    ; 
  28.             long seq = ringBuffer.next(); 
  29.             ValueEvent valueEvent = ringBuffer.get(seq); 
  30.             valueEvent.setValue(uuid); 
  31.             ringBuffer.publish(seq); 
  32.         } 
  33.         disruptor.shutdown(); 
  34.         exec.shutdown(); 
  35.     } 
package com.trevorbernard.disruptor.examples;

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

import com.lmax.disruptor.EventHandler;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;

public class Simple {
    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        ExecutorService exec = Executors.newCachedThreadPool();
        // Preallocate RingBuffer with 1024 ValueEvents
        Disruptor<ValueEvent> disruptor = new Disruptor<ValueEvent>(ValueEvent.EVENT_FACTORY, 1024, exec);
        final EventHandler<ValueEvent> handler = new EventHandler<ValueEvent>() {
            // event will eventually be recycled by the Disruptor after it wraps
            public void onEvent(final ValueEvent event, final long sequence, final boolean endOfBatch) throws Exception {
                System.out.println("Sequence: " + sequence+"   ValueEvent: " + event.getValue());
            }
        };

        disruptor.handleEventsWith(handler);
        RingBuffer<ValueEvent> ringBuffer = disruptor.start();
        for (long i = 10; i < 15; i++) {
            String uuid =String.valueOf(i)    ;
            long seq = ringBuffer.next();
            ValueEvent valueEvent = ringBuffer.get(seq);
            valueEvent.setValue(uuid);
            ringBuffer.publish(seq);
        }
        disruptor.shutdown();
        exec.shutdown();
    }
}




设置一个事件的监听,这里只是打印一下.
然后就是把我们定义的事件写入到RingBuffer,然后发布.负责监听的就会接受到.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值