高并发框架disruptor简单测试

Java中并发无外乎多线程加锁等方式,平时用的比较多的就是util.concurrency下的一些工具。除此之外业界比较推崇的就是erlang、scala中较为流行的actor模型,该模型是著名的无锁模型,actor(可以简单认为是轻量线程)之间通过发送消息进行通信,由事件驱动,全程无锁。

最近看论坛发现了另外一个并发模型-disruptor。它比较特殊,其核心是ringbuffer,闲来无事做了个简单测试。用它来模拟典型的生产者-消费者问题,本例中一个消费者-一个生产者,并将disruptor和jdk提供的ArrayBlockingQueue做了对比,发现在本人的笔记本上前者比后者快将近10倍!

 

闲话少叙,直接贴代码,如有意见或问题欢迎拍砖。

 

public class BqTest {
	static boolean ft = true;
	static boolean z = true;
	/**
	 * <b>Title:</b> main</br>
	 * <b>Description:</b> 
	 * @param args void   
	 * @throws:
	 * @author: shenbaise
	 */
	public static void main(String[] args) {
		BqTest bt = new BqTest();
		bt.test();
	}
	
	
	public void test(){
		
		long cost = System.currentTimeMillis();
		final BlockingQueue<Long> bq = new ArrayBlockingQueue<Long>(4096);
		
		Runnable p = new Runnable() {
			public void run() {
				for(int i= 0;i<100000000;i++){
					try {
						bq.put((long) i);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				ft = false;
			}
		};
		
		Runnable c = new Runnable() {
			public void run() {
				while(ft || !bq.isEmpty()){
					try {
						bq.take();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				z = false;
			}
		};
		
		new Thread(c).start();
		
		new Thread(p).start();
		
		while(z){
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		System.out.println("cost:"+(System.currentTimeMillis() - cost));
	}
	
	
}

 

disruptor的测试稍微麻烦点。

public final class ValueEvent {
	private long value;

	public long getValue() {
		return value;
	}

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

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

 

public class MyEventHandler implements EventHandler<ValueEvent>{

	public long count = 0;

	public void onEvent(ValueEvent arg0, long arg1, boolean arg2)
			throws Exception {
		arg0.getValue();
		//  为了公平这里什么都不做
	}
}

 

/**
 * @author shenbaise
 */
public class RbTest {
		
	private static final int BUFFER_SIZE = 4096;
	
	public static void main(String[] args) {
		RbTest test = new RbTest();
		test.test();
	}
	
	public void test() {
		long cost = System.currentTimeMillis();
		RingBuffer<ValueEvent> ringBuffer =
	        createSingleProducer(ValueEvent.EVENT_FACTORY, BUFFER_SIZE, 
	        		new YieldingWaitStrategy());
	    SequenceBarrier sequenceBarrier = ringBuffer.newBarrier();
	    MyEventHandler handler = new MyEventHandler();
	    BatchEventProcessor<ValueEvent> batchEventProcessor = 
	    	new BatchEventProcessor<ValueEvent>(ringBuffer, sequenceBarrier, handler);
	    ringBuffer.addGatingSequences(batchEventProcessor.getSequence());
	    ExecutorService executor = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory);
      executor.submit(batchEventProcessor);
	final RingBuffer<ValueEvent> rb = ringBuffer;
        for (long i = 0; i < 100000000; i++)
        {
            long next = rb.next();
            rb.get(next).setValue(i);
            rb.publish(next);
        }
        batchEventProcessor.halt();
        System.out.println("cost:"+(System.currentTimeMillis() - cost));
	}
}

 

最终ArrayBlockingQueue需要越20秒,而disruptor仅2秒左右。

测试环境:

写道
jdk7

主机名: WHITEME-PC
OS 名称: Microsoft Windows 7 旗舰版

系统制造商: LENOVO
系统型号: INVALID
系统类型: x64-based PC
处理器: 安装了 1 个处理器。
[01]: Intel64 Family 6 Model 37 Stepping 5 GenuineIntel ~2533 Mhz
BIOS 版本: LENOVO 2ECN29WW , 2010/9/21
物理内存总量: 3,957 MB
可用的物理内存: 987 MB
虚拟内存: 最大值: 7,911 MB
虚拟内存: 可用: 4,344 MB
虚拟内存: 使用中: 3,567 MB

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值