Disruptor是一种开源并发框架。简介:
1 能够在一个线程内每秒处理六百万个订单
2 能够在无锁的情况下实现网络的Queue并发操作
3 Disruptor是一种非常快的消息框架(轻量的JMS)也可以认为是观察者模式的一个实现
下面是对Disruptor的一个简单实现
disruptor策略:
BlockingWaitStrategy 是最低效的策略,但其对cpu的消耗最小并且在各种环境中提供更加一致的表现
SleepingWaitStrategy 的性能与BlockingWaitStrategy差不多,cpu消耗也差不多,但对生产者线程影响最小,适合异步日志类
YieldingWaitStrategy的性能是最好的,适合于低延迟的系统,在要求极高性能且事件处理线小于cpu逻辑核心数的场景中,使用此策略,例如cpu开启超线程特性(推荐使用)
Disruptor d =new Disruptor<Event(自定义的一个bean)>(EventFactory factory,ringBufferSize,executor,ProducerType,
YiedingWaitStrategy);
d.handlerEventsWith(EventHandler e); //传入一个实现了EventHandler接口的对象,在该对象重写该接口的方法中写处理逻辑
d.start();//启动disruptor
RingBuffer ringBuffer = disruptor.getRingBuffer();//得到ringBuffer对象
LongEventProducer producer = new LongEventProducer(ringBuffer);//得到生产者实例对象,生产者自定义,定义模板见下面代码
producer.onData();//填充并发布事件
DisruptorTest类
package dis;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.springframework.web.servlet.handler.DispatcherServletWebRequest;
import com.lmax.disruptor.BusySpinWaitStrategy;
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.EventHandlerGroup;
import com.lmax.disruptor.dsl.ProducerType;
public class DisruptorTest {
public static void main(String[] args) throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(10);//设置子线程准备数
//RingBuffer<LongEvent> = new R
ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() + 2);
@SuppressWarnings("deprecation")
Disruptor<LongEvent> disruptor = new Disruptor<LongEvent>(new EventFactory<LongEvent>() {
public LongEvent newInstance() {
// TODO Auto-generated method stub
return new LongEvent();
}
},1024*1024,executor, ProducerType.SINGLE, new YieldingWaitStrategy());
RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();//得到 RingBuffer
LongEventHandler1 h1 = new LongEventHandler1();
LongEventHandler2 h2 = new LongEventHandler2();
LongEventHandler3 h3 = new LongEventHandler3();
LongEventHandler4 h4 = new LongEventHandler4();
LongEventHandler5 h5 = new LongEventHandler5();
disruptor.handleEventsWith(h1,h2);//1236 1456 //六边形处理
disruptor.after(h1).handleEventsWith(h3);
disruptor.after(h2).handleEventsWith(h4);
disruptor.after(h3,h4).handleEventsWith(h5);
disruptor.start();
for(int i = 0;i < 10;i++) {
new Thread(new LongEventProducerThread(countDownLatch, ringBuffer)).start();
}
countDownLatch.await();//等待所有子线程执行完毕
disruptor.shutdown();//关闭disruptor
executor.shutdown();
}
}
LongEvent类
package dis;
public class LongEvent {
private int goodsPrice;
private String goodsName;
private int goodsId;
private String goodsType;
private String goodsSeller;
private String goodsBuyer;
public LongEvent(){
}
public int getGoodsPrice() {
return goodsPrice;
}
public void setGoodsPrice(int goodsPrice) {
this.goodsPrice = goodsPrice;
}
public String getGoodsName() {
return goodsName;
}
public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}
public int getGoodsId() {
return goodsId;
}
public void setGoodsId(int goodsId) {
this.goodsId = goodsId;
}
public String getGoodsType() {
return goodsType;
}
public void setGoodsType(String goodsType) {
this.goodsType = goodsType;
}
public String getGoodsSeller() {
return goodsSeller;
}
public void setGoodsSeller(String goodsSeller) {
this.goodsSeller = goodsSeller;
}
public String getGoodsBuyer() {
return goodsBuyer;
}
public void setGoodsBuyer(String goodsBuyer) {
this.goodsBuyer = goodsBuyer;
}
}
package dis;
import com.lmax.disruptor.EventHandler;
public class LongEventHandler0 implements EventHandler<LongEvent> {
@Override
public void onEvent(LongEvent event, long sequence, boolean endOfBatch) throws Exception {
// TODO Auto-generated method stub
System.out.println("商品Id"+event.getGoodsId());
}
}
package dis;
import com.lmax.disruptor.RingBuffer;
import com.sun.corba.se.impl.ior.ByteBuffer;
public class LongEventProducer{
RingBuffer<LongEvent> ringBuffer;
public LongEventProducer(RingBuffer<LongEvent> ringBuffer) {
this.ringBuffer = ringBuffer;
}
//发布数据
public void onData(String s) {
Long sequence = ringBuffer.next();//索取下一个事件槽
try{
LongEvent event = ringBuffer.get(sequence);//得到下一个事件槽中事件
event.setGoodsBuyer("goodsBuyer");//填充数据
event.setGoodsId(10010);
event.setGoodsName("goodsName");
event.setGoodsType("goodsType");
event.setGoodsSeller("goodsSeller");
event.setGoodsBuyer("goodsBuyer");
}catch(Exception e) {
}finally{
ringBuffer.publish(sequence);//发布事件
}
}
}
package dis;
import java.util.concurrent.CountDownLatch;
import com.lmax.disruptor.RingBuffer;
public class LongEventProducerThread implements Runnable{
public LongEventProducerThread() {
}
CountDownLatch countDownLatch;
RingBuffer<LongEvent> ringBuffer;
public LongEventProducerThread(CountDownLatch countDownLatch,RingBuffer<LongEvent> ringBuffer) {
// TODO Auto-generated constructor stub
this.countDownLatch = countDownLatch;
this.ringBuffer = ringBuffer;
}
@Override
public void run() {
LongEventProducer producer = new LongEventProducer(ringBuffer);
for(int i = 0;i < 1;i++) {
producer.onData("a");
}
countDownLatch.countDown();
}
}