誰能告訴我什麼是Disruptor設計模式與簡單的例子?我會想知道這種設計模式的基礎知識。什麼是LMAX干擾器設計模式?
5
A
回答
4
一個簡單的谷歌給了我大量的信息,包括this introduction由Martin Fowler
在粗級別,你可以把干擾器爲一體的 隊列,生產者將物體放在它的多播圖被髮送通過單獨的下游隊列向所有的消費者進行並行消費。 當你在裏面看時,你會發現這個隊列網絡真的是一個單一的數據結構 - 一個環形緩衝區。每個生產者和消費者都有一個序列計數器,用於指示當前在哪個緩衝區中的 。每個生產者/消費者編寫自己的序列計數器,但 可以讀取其他序列計數器。通過這種方式,製片人可以讀取 消費者計數器,以確保其想要寫入的插槽是 ,而且在計數器上沒有任何鎖定。類似地,消費者可以通過觀察計數器來確保它只處理消息一旦其他消費者完成 。
GitHub project包含Java代碼+ doc。
2
我花了幾天的時間閱讀所有內容,並且剛剛開始從體系結構上解決這個問題,並開始瞭解爲什麼會出現這種設計模式的原因。
對於如何實現嘗試https://github.com/trevorbernard/disruptor-examples
對於一個很好的說明,包括鏈接到一個白皮書,源代碼和UML圖,你可以嘗試開始 http://martinfowler.com/articles/lmax.html
2
從this article一個簡單的代碼示例:
干擾模式是一個批處理隊列,由一個圓形的 陣列(即環形緩衝區)填滿預先分配的傳輸 使用內存屏障的對象通過序列同步生產者和 消費者。
幸運的是,您並不需要了解破壞者模式的內在細節才能使用它。如果您發現通過代碼更容易理解,以下是的Hello World,這是一個用於實現干擾模式的線程間通信的超低延遲隊列。
package com.coralblocks.coralqueue.sample.queue;
import com.coralblocks.coralqueue.AtomicQueue;
import com.coralblocks.coralqueue.Queue;
import com.coralblocks.coralqueue.util.Builder;
public class Basics {
public static void main(String[] args) {
final Queue<StringBuilder> queue = new AtomicQueue<StringBuilder>(1024, new Builder<StringBuilder>() {
@Override
public StringBuilder newInstance() {
return new StringBuilder(1024);
}
});
Thread producer = new Thread(new Runnable() {
private final StringBuilder getStringBuilder() {
StringBuilder sb;
while((sb = queue.nextToDispatch()) == null) {
// queue can be full if the size of the queue
// is small and/or the consumer is too slow
// busy spin (you can also use a wait strategy instead)
}
return sb;
}
@Override
public void run() {
StringBuilder sb;
while(true) { // the main loop of the thread
// (...) do whatever you have to do here...
// and whenever you want to send a message to
// the other thread you can just do:
sb = getStringBuilder();
sb.setLength(0);
sb.append("Hello!");
queue.flush();
// you can also send in batches to increase throughput:
sb = getStringBuilder();
sb.setLength(0);
sb.append("Hi!");
sb = getStringBuilder();
sb.setLength(0);
sb.append("Hi again!");
queue.flush(); // dispatch the two messages above...
}
}
}, "Producer");
Thread consumer = new Thread(new Runnable() {
@Override
public void run() {
while (true) { // the main loop of the thread
// (...) do whatever you have to do here...
// and whenever you want to check if the producer
// has sent a message you just do:
long avail;
while((avail = queue.availableToPoll()) == 0) {
// queue can be empty!
// busy spin (you can also use a wait strategy instead)
}
for(int i = 0; i < avail; i++) {
StringBuilder sb = queue.poll();
// (...) do whatever you want to do with the data
// just don't call toString() to create garbage...
// copy byte-by-byte instead...
}
queue.donePolling();
}
}
}, "Consumer");
consumer.start();
producer.start();
}
}
相關問題
- 1. LMAX的干擾模式如何工作?
- 2. 什麼是LMAX干擾隊列的高性能替代品?
- 3. LMAX干擾器vs JMS提供程序
- 4. 設計模式 - 這是什麼模式?
- 5. LMAX Disruptor事件中的類字段是否需要變化?從LMAX干擾器 「入門」
- 6. 干擾模式和NServiceBus
- 7. 使用多個LMAX擾動器
- 8. 與單生產者模式相比,多生產者模式下lmax干擾者速度太慢
- 9. 這是什麼樣的設計模式?
- 10. 什麼是「調度員」設計模式?
- 11. 這是什麼JS設計模式?
- 12. 這是什麼設計模式?
- 13. 什麼是鬼魂設計模式?
- 14. 什麼是非鎖定設計模式?
- 15. 這是什麼設計模式?
- 16. 什麼是動作設計模式?
- 17. 什麼是設計模式標題?
- 18. 什麼是門面設計模式?
- 19. 這是什麼設計模式?
- 20. 什麼是四人幫設計模式
- 21. .net中的設計模式是什麼?
- 22. 是什麼意識到設計模式?
- 23. 這是什麼設計模式?
- 24. 使dust.js能夠呈現typeahead.js模板的最少干擾方式是什麼?
- 25. 干擾器和OLAP
- 26. 適配器設計模式的需求是什麼?
- 27. 設計模式:前端控制器的對立面是什麼?
- 28. 爲什麼Icomoon會干擾FontAwesome?
- 29. JQuery干擾了bokeh.js。爲什麼?
- 30. 爲什麼「#define A」會干擾「namespace A {}」?
爲什麼你需要'設計模式'?干擾器只是一個環形緩衝區隊列,有些應用程序「準則」如何避免爭用和緩存行反彈。他們使用繁忙等待來減少延遲,所有數據通常以序列化形式存儲到環形緩衝區的預先分配的內存中,因此不會發生清理。 – bestsss
我需要這個用於我的股票市場網絡應用程序(www.askkuber.com),我們有很多線程支持的處理。 –
除非你需要實時更新時間(這意味着沒有網絡),你可以依靠簡單的執行器或Fork/Join。即使這樣也沒關係。 – bestsss