java的并发框架_Java 并发框架Disruptor(七)

Disruptor VS BlockingQueue的压测对比:

import java.util.concurrent.ArrayBlockingQueue;

public class ArrayBlockingQueue4Test {

public static void main(String[] args) {

final ArrayBlockingQueue queue = new ArrayBlockingQueue(100000000);

final long startTime = System.currentTimeMillis();

//向容器中添加元素

new Thread(new Runnable() {

public void run() {

long i = 0;

while (i < Constants.EVENT_NUM_OHM) {

Data data = new Data(i, "c" + i);

try {

queue.put(data);

} catch (InterruptedException e) {

e.printStackTrace();

}

i++;

}

}

}).start();

new Thread(new Runnable() {

public void run() {

int k = 0;

while (k < Constants.EVENT_NUM_OHM) {

try {

queue.take();

} catch (InterruptedException e) {

e.printStackTrace();

}

k++;

}

long endTime = System.currentTimeMillis();

System.out.println("ArrayBlockingQueue costTime = " + (endTime - startTime) + "ms");

}

}).start();

}

}

public interface Constants {

int EVENT_NUM_OHM = 1000000;

int EVENT_NUM_FM = 50000000;

int EVENT_NUM_OM = 10000000;

}

import java.util.concurrent.Executors;

import com.lmax.disruptor.BlockingWaitStrategy;

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.ProducerType;

public class DisruptorSingle4Test {

public static void main(String[] args) {

int ringBufferSize = 65536;

final Disruptor disruptor = new Disruptor(

new EventFactory() {

public Data newInstance() {

return new Data();

}

},

ringBufferSize,

Executors.newSingleThreadExecutor(),

ProducerType.SINGLE,

//new BlockingWaitStrategy()

new YieldingWaitStrategy()

);

DataConsumer consumer = new DataConsumer();

//消费数据

disruptor.handleEventsWith(consumer);

disruptor.start();

new Thread(new Runnable() {

public void run() {

RingBuffer ringBuffer = disruptor.getRingBuffer();

for (long i = 0; i < Constants.EVENT_NUM_OHM; i++) {

long seq = ringBuffer.next();

Data data = ringBuffer.get(seq);

data.setId(i);

data.setName("c" + i);

ringBuffer.publish(seq);

}

}

}).start();

}

}

import com.lmax.disruptor.EventHandler;

public class DataConsumer implements EventHandler {

private long startTime;

private int i;

public DataConsumer() {

this.startTime = System.currentTimeMillis();

}

public void onEvent(Data data, long seq, boolean bool)

throws Exception {

i++;

if (i == Constants.EVENT_NUM_OHM) {

long endTime = System.currentTimeMillis();

System.out.println("Disruptor costTime = " + (endTime - startTime) + "ms");

}

}

}

import java.io.Serializable;

public class Data implements Serializable {

private static final long serialVersionUID = 2035546038986494352L;

private Long id ;

private String name;

public Data() {

}

public Data(Long id, String name) {

super();

this.id = id;

this.name = name;

}

public Long getId() {

return id;

}

public void setId(Long id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

BlockingQueue测试:

facd5a2b9919fff0add20037bd60f65b.png

1.建立一个工厂Event类,用于创建Event类实例对象

2.需要有一个jian监听事件类,用于处理数据(Event类)

3.实例化Disruptor实例,配置一系列参数,编写DisDisruptor核心组件

4.编写生产者组件,向Disruptor容器中投递数据

pom.xml添加:

com.lmax

disruptor

3.3.2

public class OrderEvent {

private long value; //订单的价格

public long getValue() {

return value;

}

public void setValue(long value) {

this.value = value;

}

}

import com.lmax.disruptor.EventFactory;

public class OrderEventFactory implements EventFactory{

public OrderEvent newInstance() {

return new OrderEvent();//这个方法就是为了返回空的数据对象(Event)

}

}

public class OrderEventHandler implements EventHandler{

public void onEvent(OrderEvent event, long sequence, boolean endOfBatch) throws Exception {

Thread.sleep(Integer.MAX_VALUE);

System.err.println("消费者: " + event.getValue());

}

}

import java.nio.ByteBuffer;

import com.lmax.disruptor.RingBuffer;

public class OrderEventProducer {

private RingBuffer ringBuffer;

public OrderEventProducer(RingBuffer ringBuffer) {

this.ringBuffer = ringBuffer;

}

public void sendData(ByteBuffer data) {

//1 在生产者发送消息的时候, 首先 需要从我们的ringBuffer里面 获取一个可用的序号

long sequence = ringBuffer.next();//0

try {

//2 根据这个序号, 找到具体的 "OrderEvent" 元素 注意:此时获取的OrderEvent对象是一个没有被赋值的"空对象"

OrderEvent event = ringBuffer.get(sequence);

//3 进行实际的赋值处理

event.setValue(data.getLong(0));

} finally {

//4 提交发布操作

ringBuffer.publish(sequence);

}

}

}

import java.nio.ByteBuffer;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import com.lmax.disruptor.BlockingWaitStrategy;

import com.lmax.disruptor.RingBuffer;

import com.lmax.disruptor.dsl.Disruptor;

import com.lmax.disruptor.dsl.ProducerType;

public class Main {

public static void main(String[] args) {

// 参数准备工作

OrderEventFactory orderEventFactory = new OrderEventFactory();

int ringBufferSize = 4;

ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());

/**

* 1 eventFactory: 消息(event)工厂对象

* 2 ringBufferSize: 容器的长度

* 3 executor: 线程池(建议使用自定义线程池) RejectedExecutionHandler

* 4 ProducerType: 单生产者 还是 多生产者

* 5 waitStrategy: 等待策略

*/

//1. 实例化disruptor对象

Disruptor disruptor = new Disruptor(orderEventFactory,

ringBufferSize,

executor,

ProducerType.SINGLE,

new BlockingWaitStrategy());

//2. 添加消费者的监听 (构建disruptor 与 消费者的一个关联关系)

disruptor.handleEventsWith(new OrderEventHandler());

//3. 启动disruptor

disruptor.start();

//4. 获取实际存储数据的容器: RingBuffer

RingBuffer ringBuffer = disruptor.getRingBuffer();

OrderEventProducer producer = new OrderEventProducer(ringBuffer);

ByteBuffer bb = ByteBuffer.allocate(8);

for(long i = 0 ; i < 100; i ++){

bb.putLong(0, i);

producer.sendData(bb);

}

disruptor.shutdown();

executor.shutdown();

}

}

f031410a34161f406d523141e4f19834.png

ee34f0aa1d56e421fca83ee992a53a9b.png

547979d464a2153527f6dc644a0d2dde.png

7835ac26bb7180504e05ed2f2266d801.png

679f1347b178826e4192c341f3a2d5b8.png

571d7801b217faade3e37ab3434a30a6.png

3eb2e4bfe22f18d5e7976a14110d0ca8.png

aa9148339b99f29b762ebb6ffee8d41e.png

8bf2a543038bf5962f2445b320476793.png

9031cfc0245c2baa5b21f459e7bc06b1.png

2039d5d8696f407933e012b2aa26f58d.png

5ac8e0be21ac7b621f1f27877955a302.png

50e42a8d1e060746d79f4b0b563f7951.png

48a003ff80ab7abf3dbdc25c315d4acb.png

791299fb4d2ee67550945cc7cdde223a.png

e2a19a8559d65a44ded468db003bbee5.png

3d6bea7dd99c86c64d2a37a22aabd6ad.png

public final class BlockingWaitStrategy implements WaitStrategy

{

private final Lock lock = new ReentrantLock();

private final Condition processorNotifyCondition = lock.newCondition();

@Override

public long waitFor(long sequence, Sequence cursorSequence, Sequence dependentSequence, SequenceBarrier barrier)

throws AlertException, InterruptedException

{

long availableSequence;

if ((availableSequence = cursorSequence.get()) < sequence)

{

lock.lock();

try

{

while ((availableSequence = cursorSequence.get()) < sequence)

{

barrier.checkAlert();

processorNotifyCondition.await();

}

}

finally

{

lock.unlock();

}

}

while ((availableSequence = dependentSequence.get()) < sequence)

{

barrier.checkAlert();

}

return availableSequence;

}

@Override

public void signalAllWhenBlocking()

{

lock.lock();

try

{

processorNotifyCondition.signalAll();

}

finally

{

lock.unlock();

}

}

}

public final class SleepingWaitStrategy implements WaitStrategy

{

private static final int DEFAULT_RETRIES = 200;

private final int retries;

public SleepingWaitStrategy()

{

this(DEFAULT_RETRIES);

}

public SleepingWaitStrategy(int retries)

{

this.retries = retries;

}

@Override

public long waitFor(final long sequence, Sequence cursor, final Sequence dependentSequence, final SequenceBarrier barrier)

throws AlertException, InterruptedException

{

long availableSequence;

int counter = retries;

while ((availableSequence = dependentSequence.get()) < sequence)

{

counter = applyWaitMethod(barrier, counter);

}

return availableSequence;

}

@Override

public void signalAllWhenBlocking()

{

}

private int applyWaitMethod(final SequenceBarrier barrier, int counter)

throws AlertException

{

barrier.checkAlert();

if (counter > 100)

{

--counter;

}

else if (counter > 0)

{

--counter;

Thread.yield();

}

else

{

LockSupport.parkNanos(1L);

}

return counter;

}

}

public final class YieldingWaitStrategy implements WaitStrategy

{

private static final int SPIN_TRIES = 100;

@Override

public long waitFor(final long sequence, Sequence cursor, final Sequence dependentSequence, final SequenceBarrier barrier)

throws AlertException, InterruptedException

{

long availableSequence;

int counter = SPIN_TRIES;

while ((availableSequence = dependentSequence.get()) < sequence)

{

counter = applyWaitMethod(barrier, counter);

}

return availableSequence;

}

@Override

public void signalAllWhenBlocking()

{

}

private int applyWaitMethod(final SequenceBarrier barrier, int counter)

throws AlertException

{

barrier.checkAlert();

if (0 == counter)

{

Thread.yield();

}

else

{

--counter;

}

return counter;

}

}

ca2e267b1c17efc5e7d97c01e5aad5ab.png

84f13ad725c51eb110def8c55e9b29b8.png

f14bbc6aff7997bfabed260d23a489fb.png

ed189d29495ed24e0d65bb576b335383.png

c867560ee4d187927809e76e4b9b0379.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值