Disruptor学习----综述

Disruptor学习----综述

Disruptor是一个高效无锁消息队列,由LMAX公司开发并应用于金融交易,性能极高(https://github.com/LMAX-Exchange/disruptor)。

Disruptor设计的很巧妙运用(cas,cpu cache对数组友好支持等),感觉非常值得大家学下。Disruptor以其无锁实现生产者/消费者队列,带来了高性能,比锁快多,意味着锁的性能不理想。下面贴下disruptor文档中所展示的数据,锁啊,额滴神,这性能。

Method

Time (ms)

Single thread

300

Single thread with lock

10,000

Two threads with lock

224,000

Single thread with CAS

5,700

Two threads with CAS

30,000

Single thread with volatile write

4,700

其和兴就是RingBuffer


Disruptor学习----综述_第1张图片


基本用法:

Disruptor disruptor = newDisruptor(MyEvent.FACTORY, 32, Executors.newCachedThreadPool());
EventHandler handler1 = newEventHandler() { ... };
EventHandler handler2 = newEventHandler() { ... };
disruptor.handleEventsWith(handler1);
disruptor.after(handler1).handleEventsWith(handler2);
RingBuffer ringBuffer = disruptor.start();





你可能感兴趣的:(JAVA,Disruptor)