设想一个例子,随机生成一个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) {
}
}
}