Java并发框架Disruptor教程(三)、单一生产者_disruptor3 教程(1)

总结

在清楚了各个大厂的面试重点之后,就能很好的提高你刷题以及面试准备的效率,接下来小编也为大家准备了最新的互联网大厂资料。

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

    // Connect the handler
    disruptor.handleEventsWith(LongEventMain::handleEvent);

    // Start the Disruptor, starts all threads running
    disruptor.start();

    // Get the ring buffer from the Disruptor to be used for publishing.
    RingBuffer<TradeEvent> ringBuffer = disruptor.getRingBuffer();

    ByteBuffer bb = ByteBuffer.allocate(4);
    for (int l = 0; true; l++)
    {
        bb.putInt(0, l);
        ringBuffer.publishEvent(LongEventMain::translate, bb);
        Thread.sleep(1000);
    }
}

}


### 多个消费者


* **串行**  
 消费者之间必须等待上一个执行完成才能继续  
 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20190306104156471.png)  
 需求:C1、添加订单号;C2、记录订单(入库);C3、打印订单详情  
 代码实现:



public class LongEventMain1 {
public static void handleEvent1(TradeEvent event, long sequence, boolean endOfBatch) {
event.setId(UUID.randomUUID().toString());
}

public static void handleEvent2(TradeEvent event, long sequence, boolean endOfBatch) {
    System.out.println(String.format("订单号:%s,入库成功!!", event.getId()));
}

public static void handleEvent(TradeEvent event, long sequence, boolean endOfBatch) {
    System.out.println(event);
}

public static void translate(TradeEvent event, long sequence, ByteBuffer buffer) {
    event.setUserId(buffer.getInt(0));
}

public static void main(String[] args) throws Exception {
    // Specify the size of the ring buffer, must be power of 2.
    int bufferSize = 1024;

    // Construct the Disruptor
    Disruptor<TradeEvent> disruptor = new Disruptor<>(TradeEvent::new, bufferSize, DaemonThreadFactory.INSTANCE);

    // Connect the handler
    disruptor.handleEventsWith(LongEventMain1::handleEvent1)
            .handleEventsWith(LongEventMain1::handleEvent2)
            .handleEventsWith(LongEventMain1::handleEvent);

    // Start the Disruptor, starts all threads running
    disruptor.start();

    // Get the ring buffer from the Disruptor to be used for publishing.
    RingBuffer<TradeEvent> ringBuffer = disruptor.getRingBuffer();

    ByteBuffer bb = ByteBuffer.allocate(8);
    for (int l = 0; true; l++) {
        bb.putInt(0, l);
        ringBuffer.publishEvent(LongEventMain1::translate, bb);
        Thread.sleep(1000);
    }
}

}


* **并行**  
 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20190306112812897.png)  
 需求:  
 C1、添加订单状态  
 C2、添加订单名称  
 C3、打印订单详情  
 代码实现:



import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.util.DaemonThreadFactory;

import java.nio.ByteBuffer;
import java.util.Random;
import java.util.UUID;

public class LongEventMain2 {
private static Random random = new Random();

public static void handleEvent1(TradeEvent event, long sequence, boolean endOfBatch) {
    event.setId(UUID.randomUUID().toString());
    try {
        int i = random.nextInt(10);
        Thread.sleep(i);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println("====ID====");
}

public static void handleEvent2(TradeEvent event, long sequence, boolean endOfBatch) {
    event.setName("order:"+event.getUserId());
    try {
        int i = random.nextInt(10);
        Thread.sleep(i);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println("====Name====");
}

public static void handleEvent(TradeEvent event, long sequence, boolean endOfBatch) {
    System.out.println(event);
}

public static void translate(TradeEvent event, long sequence, ByteBuffer buffer) {
    event.setUserId(buffer.getInt(0));
}

public static void main(String[] args) throws Exception {
    // Specify the size of the ring buffer, must be power of 2.
    int bufferSize = 1024;

    // Construct the Disruptor
    Disruptor<TradeEvent> disruptor = new Disruptor<>(TradeEvent::new, bufferSize, DaemonThreadFactory.INSTANCE);

    // Connect the handler
    disruptor.handleEventsWith(LongEventMain2::handleEvent1,LongEventMain2::handleEvent2)
            .then(LongEventMain2::handleEvent);

    // Start the Disruptor, starts all threads running
    disruptor.start();

    // Get the ring buffer from the Disruptor to be used for publishing.
    RingBuffer<TradeEvent> ringBuffer = disruptor.getRingBuffer();

    ByteBuffer bb = ByteBuffer.allocate(8);
    for (int l = 0; true; l++) {
        bb.putInt(0, l);
        ringBuffer.publishEvent(LongEventMain2::translate, bb);
        Thread.sleep(1000);
    }
}

}


* **并行加串行**  
 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20190306113830531.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zNzkxMDQ1Mw==,size_16,color_FFFFFF,t_70)  
 需求:  
 C1、生成订单号  
 C2、生成订单名称  
 C3、记录订单号的状态  
 C4、打印订单详情  
 代码实现:



import com.lmax.disruptor.EventHandler;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.util.DaemonThreadFactory;

import java.nio.ByteBuffer;
import java.util.Random;
import java.util.UUID;

public class LongEventMain3 {
private static Random random = new Random();

public static void handleEvent1(TradeEvent event, long sequence, boolean endOfBatch) {
    event.setId(UUID.randomUUID().toString());
    try {
        int i = random.nextInt(10);
        Thread.sleep(i);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println("====ID====");
}
public static void handleEvent3(TradeEvent event, long sequence, boolean endOfBatch) {
    event.setStatus(event.getId().hashCode()%2);
    System.out.println("====Status====");
}

public static void handleEvent2(TradeEvent event, long sequence, boolean endOfBatch) {
    event.setName("order:"+event.getUserId());
    try {
        int i = random.nextInt(10);
        Thread.sleep(i);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println("====Name====");
}

public static void handleEvent(TradeEvent event, long sequence, boolean endOfBatch) {
    System.out.println(event);
}

public static void translate(TradeEvent event, long sequence, ByteBuffer buffer) {
    event.setUserId(buffer.getInt(0));
}

面试结束复盘查漏补缺

每次面试都是检验自己知识与技术实力的一次机会,面试结束后建议大家及时总结复盘,查漏补缺,然后有针对性地进行学习,既能提高下一场面试的成功概率,还能增加自己的技术知识栈储备,可谓是一举两得。

以下最新总结的阿里P6资深Java必考题范围和答案,包含最全MySQL、Redis、Java并发编程等等面试题和答案,用于参考~

重要的事说三遍,关注+关注+关注!

历经30天,说说我的支付宝4面+美团4面+拼多多四面,侥幸全获Offer

image.png

更多笔记分享

历经30天,说说我的支付宝4面+美团4面+拼多多四面,侥幸全获Offer

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

.(img-6aF2C961-1715297861456)]

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值