Disruptor处理(计算密集型)并发1000*6000的数据例子

设想一个例子,随机生成一个1-100的随机数,每个数都执行一个乘以10的操作(计算密集型的并发),会使用多少时间呢?

如下,执行这个使用了两分多钟。

 

(1)设置一个order类

public class Order {
	private String id;//ID
    private String name;
    private double price;//金额

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }

}

(2)设置一个producer类

public class Producer {
	private final RingBuffer<Order> ringBuffer;

    public Producer(RingBuffer<Order> ringBuffer){
        this.ringBuffer=ringBuffer;
    }

    /**
     * onData用来发布事件,每调用一次就发布一次事件
     * 它的参数会通过事件传递给消费者
     * @param data
     */
    public void onData(String data,double price){
        //可以把ringBuffer看作是一个事件队列,那么next就是得到下一个事件槽
        long sequence = ringBuffer.next();
        try {
            Order order = ringBuffer.get(sequence);
            order.setId(data);
            order.setPrice(price);
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            //发布事件
            ringBuffer.publish(sequence);
        }
    }

}

(3)设置一个consumer类

public class Producer {
	private final RingBuffer<Order> ringBuffer;

    public Producer(RingBuffer<Order> ringBuffer){
        this.ringBuffer=ringBuffer;
    }

    /**
     * onData用来发布事件,每调用一次就发布一次事件
     * 它的参数会通过事件传递给消费者
     * @param data
     */
    public void onData(String data,double price){
        //可以把ringBuffer看作是一个事件队列,那么next就是得到下一个事件槽
        long sequence = ringBuffer.next();
        try {
            Order order = ringBuffer.get(sequence);
            order.setId(data);
            order.setPrice(price);
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            //发布事件
            ringBuffer.publish(sequence);
        }
    }

}

(4)主函数执行

package com.disruptor.demo.moreorder;

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

import com.lmax.disruptor.EventFactory;
import com.lmax.disruptor.ExceptionHandler;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.SequenceBarrier;
import com.lmax.disruptor.WorkerPool;
import com.lmax.disruptor.YieldingWaitStrategy;
import com.lmax.disruptor.dsl.ProducerType;

public class Main {

	public static void main(String[] args) throws InterruptedException {
		// 创建ringBuffer
		RingBuffer<Order> ringBuffer = RingBuffer.create(ProducerType.MULTI, new EventFactory<Order>() {
			@Override
			public Order newInstance() {
				return new Order();
			}
		}, 1024 * 1024, new YieldingWaitStrategy());

		SequenceBarrier barrier = ringBuffer.newBarrier();

		Consumer[] consumers = new Consumer[2];
		for (int i = 0; i < consumers.length; i++) {
			consumers[i] = new Consumer("c" + i);
		}

		WorkerPool<Order> workerPool = new WorkerPool<Order>(ringBuffer, barrier, new IntEventExceptionHandler(),consumers);
		ringBuffer.addGatingSequences(workerPool.getWorkerSequences());
		workerPool.start(Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()));

		final CountDownLatch latch = new CountDownLatch(1);
		for (int i = 0; i < 1000; i++) {
			final Producer p = new Producer(ringBuffer);
			new Thread(new Runnable() {
				@Override
				public void run() {
					try {
						latch.await();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					for (int j = 0; j < 6000; j++) {
						double d = 1 + Math.random() * (100 - 1);
						p.onData(UUID.randomUUID().toString(),d);
					}
				}
			}).start();
		}
		Thread.sleep(2000);
		System.out.println("------------开始-------------");
		
		latch.countDown();
		
		Thread.sleep(130000);
		
		System.out.println("总数:" + consumers[0].getCount());

	}

	static class IntEventExceptionHandler implements ExceptionHandler {
		@Override
		public void handleEventException(Throwable arg0, long arg1, Object arg2) {
		}

		@Override
		public void handleOnShutdownException(Throwable arg0) {
		}

		@Override
		public void handleOnStartException(Throwable arg0) {
		}
	}

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值