Disruptor的概述與使用
一概述
1.Disruptor
Disruptor是一個高效能的非同步處理框架,一個「生產者-消費者」模型。
2.RingBuffer
RingBuffer是一種環形資料結構,包含一個指向下一個槽點的序號,可以在執行緒間傳遞數據。
3.Event
在Disruptor框架中,生產者生產的資料叫做Event。
二 Disruptor框架基本上構成
# 1.MyEvent:自訂對象,充當「生產者-消費者」模型中的資料。
2.MyEventFactory:實作EventFactory的接口,用於生產資料。
3.MyEventProducerWithTranslator:將資料儲存到自訂物件中並發布。
4.MyEventHandler:自訂消費者。
三Demo
初次接觸Disruptor,認識停留在表面,零散,模糊,在此記一個簡單的示例,以便日後深入研究。
1.自訂資料類別
package com.disruptor.basic;public class LongEvent {private long value;public long getValue() {return value; }public void setValue(long value) {this.value = value; } }
2.資料生產工廠(創建資料類別物件)
package com.disruptor.basic;import com.lmax.disruptor.EventFactory;public class LongEventFactory implements EventFactory<LongEvent> {public LongEvent newInstance() {// TODO Auto-generated method stubreturn new LongEvent(); } }
3.資料來源(初始化資料物件並發布)
package com.disruptor.basic;import java.nio.ByteBuffer;import com.lmax.disruptor.EventTranslatorOneArg;import com.lmax.disruptor.RingBuffer;public class LongEventProducerWithTranslator {private final RingBuffer<LongEvent> ringBuffer;public LongEventProducerWithTranslator(RingBuffer<LongEvent> ringBuffer) {this.ringBuffer = ringBuffer; }private final EventTranslatorOneArg<LongEvent, ByteBuffer> TRANSLATOR = new EventTranslatorOneArg<LongEvent, ByteBuffer>() {/** * event:包含有消费数据的对象; sequence:分配给目标对象的RingBuffer空间序号; * bb:包含有将要被存储到目标对象中的数据的容器 */public void translateTo(LongEvent event, long sequence, ByteBuffer bb) {// TODO Auto-generated method stubevent.setValue(bb.getLong(0));// 将数据存储到目标对象中 } };public void onData(ByteBuffer bb) { ringBuffer.publishEvent(TRANSLATOR, bb);// 发布,将数据推送给消费者 } }
4.消費者
package com.disruptor.basic;import com.lmax.disruptor.EventHandler;public class LongEventHandler implements EventHandler<LongEvent> {public void onEvent(LongEvent event, long sequence, boolean endOfBatch) throws Exception {// TODO Auto-generated method stubSystem.out.println("当前消费的数据="+event.getValue()); } }
5.測試類別
package com.disruptor.basic;import java.nio.ByteBuffer;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import org.junit.Test;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 LongEventTest { @SuppressWarnings({ "unchecked", "deprecation" }) @Testpublic void test01() throws InterruptedException { ExecutorService executor = Executors.newCachedThreadPool(); EventFactory<LongEvent> factory = new LongEventFactory();int bufferSize = 1024; Disruptor<LongEvent> disruptor = new Disruptor<LongEvent>(factory, bufferSize, executor, ProducerType.SINGLE,new YieldingWaitStrategy()); disruptor.handleEventsWith(new LongEventHandler()); disruptor.start(); RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();// LongEventProducer producer = new// LongEventProducer(ringBuffer);LongEventProducerWithTranslator producer = new LongEventProducerWithTranslator(ringBuffer); ByteBuffer bb = ByteBuffer.allocate(8);// long startTime = System.currentTimeMillis();for (long a = 0; a < 100; a++) { bb.putLong(0, a); producer.onData(bb);/*if (a == 99) { long endTime = System.currentTimeMillis(); System.out.println("useTime=" + (endTime - startTime)); }*/Thread.sleep(100); }/*long endTime = System.currentTimeMillis(); System.out.println("useTime=" + (endTime - startTime));*/disruptor.shutdown(); executor.shutdown(); }/*@Test public void test02() { long startTime = System.currentTimeMillis(); for (long a = 0; a < 100; a++) { System.out.println(a); } long endTime = System.currentTimeMillis(); System.out.println("useTime=" + (endTime - startTime)); }*/}
以上是Disruptor的概述與使用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

公司安全軟件導致部分應用無法正常運行的排查與解決方法許多公司為了保障內部網絡安全,會部署安全軟件。 ...

將姓名轉換為數字以實現排序的解決方案在許多應用場景中,用戶可能需要在群組中進行排序,尤其是在一個用...

系統對接中的字段映射處理在進行系統對接時,常常會遇到一個棘手的問題:如何將A系統的接口字段有效地映�...

在使用IntelliJIDEAUltimate版本啟動Spring...

在使用MyBatis-Plus或其他ORM框架進行數據庫操作時,經常需要根據實體類的屬性名構造查詢條件。如果每次都手動...

Java對象與數組的轉換:深入探討強制類型轉換的風險與正確方法很多Java初學者會遇到將一個對象轉換成數組的�...

電商平台SKU和SPU表設計詳解本文將探討電商平台中SKU和SPU的數據庫設計問題,特別是如何處理用戶自定義銷售屬...

Redis緩存方案如何實現產品排行榜列表的需求?在開發過程中,我們常常需要處理排行榜的需求,例如展示一個�...
