簡體   English   中英

如何找到Lmax Disruptor鑽石(一位生產者5位消費者1位結論)的示例?

[英]how to find a Lmax Disruptor diamond(one producer 5 consumer 1 conclude)example?

我發現github中的Lmax Disruptor用戶指南非常簡單,現在我遇到一個生產者和五個消費用戶的問題,此后我需要得出消費者的結論,是否有任何演示,如何找到Lmax Disruptor鑽石(一個生產者5消費者1得出結論)示例?

非常感謝!

您可以通過varags向Disruptor.handleEventsWith提供多個使用者。 此后注冊的呼叫結束then (流利DSL)。 第二個調用確保事件在傳遞到結束步驟之前由所有使用者處理。

一個有效的示例如下所示:

import com.lmax.disruptor.*;
import com.lmax.disruptor.dsl.*;
import java.util.concurrent.*;

public class Diamond {

    public static void main(String[] args) {
        ExecutorService executor = Executors.newCachedThreadPool();
        Disruptor<LongEvent> disruptor = new Disruptor<>(LongEvent::new, 1024, executor, ProducerType.SINGLE, new SleepingWaitStrategy());

        //register five consumers and a final conclude
        disruptor.handleEventsWith(new Consumer(1), new Consumer(2), new Consumer(3), new Consumer(4), new Consumer(5)).then(new Conclude());

        disruptor.start();

        for (int i = 0; i < 3; i++) {
            disruptor.publishEvent((event, sequence, newValue) -> event.setValue(newValue), i);
        }

        disruptor.shutdown();
        executor.shutdown();
    }

    public static class Consumer implements EventHandler<LongEvent> {
        private int i;
        public Consumer(int i) { this.i = i; }

        @Override
        public void onEvent(LongEvent event, long sequence, boolean endOfBatch) throws Exception {
            System.out.println("Consumer: " + i);
            event.setValue(event.getValue() + 1);
        }
    }

    public static class Conclude implements EventHandler<LongEvent> {
        @Override
        public void onEvent(LongEvent event, long sequence, boolean endOfBatch) throws Exception {
            System.out.println("Conclude: " + event.getValue());
        }
    }

    public static class LongEvent
    {
        private long value;

        public void setValue(long value)
        {
            this.value = value;
        }

        public long getValue() {
            return this.value;
        }
    }
}

這些事件僅包含一個長值。 使用者增加值,最后一步將其打印出來。 for循環將三個初始值為1、2和3的事件放入環中。

請注意,您不需要在工作同步LongEventConsumer的ringbuffer確保只有一個處理程序工作的一個事件一次。 此外,請注意消費者的照片在幾次運行中如何變化。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM