Disruptor-07 中有哪些代码范例
发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,这篇文章将为大家详细讲解有关Disruptor-07 中有哪些代码范例,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。public class Test
千家信息网最后更新 2025年12月02日Disruptor-07 中有哪些代码范例
这篇文章将为大家详细讲解有关Disruptor-07 中有哪些代码范例,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
public class Test { private static Logger logger = LogManager.getLogger(); @SuppressWarnings("unchecked") public static void main(String[] args) throws InterruptedException { // The factory for the event TestEventFactory factory = new TestEventFactory(); // Specify the size of the ring buffer, must be power of 2. int bufferSize = 1024; // Construct the Disruptor Disruptor disruptor = new Disruptor(factory, bufferSize, DaemonThreadFactory.INSTANCE, ProducerType.SINGLE, new BlockingWaitStrategy()); // Create EventHandler TestEventHandler handler1 = new TestEventHandler("handler1"); TestEventHandler handler2 = new TestEventHandler("handler2"); TestEventHandler handler3 = new TestEventHandler("handler3"); TestEventHandler handler4 = new TestEventHandler("handler4"); // Connect the handler int count = 100; // Unicast采取WorkPool方式,3个WorkHandler 累计执行100次。 // Event到达时,哪个WorkHandler被调度不确定。 // disruptor.handleEventsWithWorkerPool(handler1, handler2, handler3); // MulticastTest并发处理方式,3个EventHandler,各执行100次,累计300次; // 每个Event到达时,EventHandler的处理顺序不确定。 // 并发( handler1, handler2, handler3) // EventHandler:handler1--85:k-v // EventHandler:handler3--85:k-v // EventHandler:handler2--85:k-v // EventHandler:handler1--86:k-v // EventHandler:handler3--86:k-v // EventHandler:handler2--86:k-v <-----并发顺序不确定 // EventHandler:handler2--87:k-v <-----并发顺序不确定 // EventHandler:handler1--87:k-v // EventHandler:handler3--87:k-v // EventHandler:handler3--88:k-v // EventHandler:handler2--88:k-v // EventHandler:handler1--88:k-v // disruptor.handleEventsWith(handler1, handler2, handler3); // Pipeline串行处理方式,3个EventHandler,各执行100次,累计300次。 // 每个Event到达时,EventHandler的处理顺序与handleEventsWith的顺序一致。 // 顺序:handler1->handler2->handler3 // EventHandler:handler1--97:k-v // EventHandler:handler2--97:k-v // EventHandler:handler3--97:k-v // EventHandler:handler1--98:k-v // EventHandler:handler2--98:k-v // EventHandler:handler3--98:k-v // EventHandler:handler1--99:k-v // EventHandler:handler2--99:k-v // EventHandler:handler3--99:k-v // EventHandler:handler1--100:k-v // EventHandler:handler2--100:k-v // EventHandler:handler3--100:k-v //disruptor.handleEventsWith(handler1).handleEventsWith(handler2).handleEventsWith(handler3); //Diamond //按照 handler1-> 并发(handler2, hander3) ->handler4 调度 disruptor.handleEventsWith(handler1).handleEventsWith(handler2,handler3).handleEventsWith(handler4); // Start the Disruptor, starts all threads running disruptor.start(); // Get the ring buffer from the Disruptor to be used for publishing. RingBuffer ringBuffer = disruptor.getRingBuffer(); TestEventProducer producer = new TestEventProducer(ringBuffer); for (int i = 1; i <= count; i++) { producer.onEvent("k", "v"); Thread.sleep(100); } Thread.sleep(10000); }} public class TestEvent implements Event { private String key; private String value; public String getKey() { return key; } public void setKey(String key) { this.key = key; } public String getValue() { return value; } public void setValue(String value) { this.value = value; }}package com.lands.disruptor.unicast;import com.lmax.disruptor.EventFactory;public class TestEventFactory implements EventFactory{ public TestEvent newInstance() { return new TestEvent(); }}
package com.lands.disruptor.unicast;import java.util.concurrent.atomic.AtomicInteger;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import com.lmax.disruptor.EventHandler;import com.lmax.disruptor.WorkHandler;public class TestEventHandler implements EventHandler, WorkHandler { private static Logger logger = LogManager.getLogger(); private String handlerName; private AtomicInteger count = new AtomicInteger(); public TestEventHandler(String name) { this.handlerName = name; } public String getHandlerName() { return handlerName; } public void onEvent(TestEvent event) throws Exception { logger.info("WorkHandler:" + this.handlerName + "-" + count.decrementAndGet() + ":" + event.getKey() + "-" + event.getValue()); //Thread.sleep(100); } public void onEvent(TestEvent event, long sequence, boolean endOfBatch) throws Exception { logger.info("EventHandler:" + this.handlerName + "-" + count.decrementAndGet() + ":" + event.getKey() + "-" + event.getValue()); //Thread.sleep(100); }}
package com.lands.disruptor.unicast;import com.lands.disruptor.EventProducer;import com.lmax.disruptor.RingBuffer;public class TestEventProducer extends EventProducer{ public TestEventProducer(RingBuffer ringBuffer) { super(ringBuffer); } @Override public void process(TestEvent event, String... data) { event.setKey(data[0]); event.setValue(data[1]); }}
关于Disruptor-07 中有哪些代码范例就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
代码
范例
内容
文章
方式
更多
知识
篇文章
顺序
处理
调度
不错
质量
参考
帮助
有关
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
北京理工大学计算机网络技术
在哪搜索服务器我的世界
网络安全课堂第四期
面向对象 数据库 设计
北辰区网络安全
完美世界网络技术有限公
湖北服务器电源都有哪些
软件开发计算思维基础
网络安全培训新策略
四川铁人三项网络安全
计算机网络技术做什么
闪讯网络技术有限公司
商丘市网络安全课堂
三明抠喂网络技术有限公司
苏州数据库工程师招聘
黑龙江专业软件开发服务参考价格
软件开发比软件测试更重要
现代软件开发公司的网络设计
戴尔服务器黄灯
计算机网络技术课程诊改
数据库向下兼容么
热血传奇怎么提高服务器人数
如何搭建英国服务器
人保财险软件开发中心总经理
制造企业统一软件开发平台
网络技术管理制度大全
河北润迪网络技术有限公司的橱柜
嘉兴软件开发自学步骤
上海ar软件开发报价
刀片服务器巡检报告