首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >什么是LMAX Disruptor设计模式?

什么是LMAX Disruptor设计模式?
EN

Stack Overflow用户
提问于 2013-02-01 00:48:18
回答 3查看 4.7K关注 0票数 5

谁能用简单的例子告诉我什么是Disruptor设计模式?我想知道这个设计模式的基础知识。

EN

回答 3

Stack Overflow用户

发布于 2013-02-01 00:50:30

一个简单的谷歌给了我很多信息,包括Martin Fowler的this introduction

在粗略的级别上,您可以将

看作是队列的多播图,生产者将对象放在上面,然后通过单独的下游队列将这些对象发送给所有消费者进行并行消费。当你深入观察时,你会发现这个队列网络实际上是一个单一的数据结构--一个环形缓冲区。每个生产者和消费者都有一个顺序计数器来指示它当前正在处理缓冲区中的哪个槽。每个生产者/消费者写入其自己的序列计数器,但可以读取其他生产者/消费者的序列计数器。这样,生产者可以读取消费者的计数器,以确保它想要写入的插槽是可用的,而不会对计数器产生任何锁定。类似地,一个消费者可以通过观察计数器来确保它只在另一个消费者处理完消息后才处理它。

GitHub project包含Java代码+文档。

票数 4
EN

Stack Overflow用户

发布于 2013-09-10 14:55:32

我花了几天的时间阅读它,并开始从架构上掌握它,以及为什么会出现这种设计模式的原因。

有关如何实现https://github.com/trevorbernard/disruptor-examples的简单代码示例,请尝试

要获得良好的描述,包括白皮书、源代码和UML图的链接,可以尝试从http://martinfowler.com/articles/lmax.html开始。

票数 3
EN

Stack Overflow用户

发布于 2014-05-24 08:09:56

来自this article

中断模式是一个批处理队列,由一个充满预分配传输对象的循环数组(即环形缓冲区)支持,该循环数组使用内存屏障通过序列同步生产者和消费者。

幸运的是,您不需要了解破坏性模式的内在细节就可以使用它。如果您发现通过代码更容易理解,下面是CoralQueue的Hello World,这是一个用于线程间通信的超低延迟队列,它实现了中断模式。

代码语言:javascript
复制
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();
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14630901

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档