I want to fly higher
          programming Explorer
          posts - 114,comments - 263,trackbacks - 0
          <2015年6月>
          31123456
          78910111213
          14151617181920
          21222324252627
          2829301234
          567891011

          常用鏈接

          留言簿(5)

          隨筆分類(161)

          隨筆檔案(114)

          文章分類(2)

          文章檔案(2)

          Alibaba

          Comprehensive

          Expert

          Game

          Java

          搜索

          •  

          積分與排名

          • 積分 - 599964
          • 排名 - 78

          最新評論

          閱讀排行榜

          package com.mavsplus.example.disruptor;

          import java.nio.ByteBuffer;
          import java.util.concurrent.Executor;
          import java.util.concurrent.Executors;

          import com.lmax.disruptor.RingBuffer;
          import com.lmax.disruptor.dsl.Disruptor;

          /**
           * Distrupor Getting Started
           * 
           * <a
           * href="https://github.com/LMAX-Exchange/disruptor/wiki/Getting-Started"></a>
           * 
           * @author landon
           * @since 1.8.0_25
           
          */
          public class DisruptorExample {

              /**
               * the Event that will carry the data
               
          */
              public static class LongEvent {

                  private long value;

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

                  @Override
                  public String toString() {
                      return "LongEvent [value=" + value + "]";
                  }
              }

              public static void main(String[] args) throws Exception {
                  // usingJava8();
                  usingJava8Another();
              }

              // Using Java8
              @SuppressWarnings("unchecked")
              private static void usingJava8() throws InterruptedException {
                  // Executor that will be used to construct new threads for consumers
                  Executor executor = Executors.newCachedThreadPool();

                  // Specify the size of the ring buffer, must be power of 2.
                  int bufferSize = 1024;

                  // Construct the Disruptor
                  Disruptor<LongEvent> disruptor = new Disruptor<>(LongEvent::new, bufferSize, executor);
                  // Connect the handler
                  disruptor.handleEventsWith((event, sequence, endOfBatch) -> System.out.println("Event: " + event));
                  // Start the Disruptor, starts all threads running
                  disruptor.start();

                  // Get the ring buffer from the Disruptor to be used for publishing.
                  RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();

                  ByteBuffer bb = ByteBuffer.allocate(8);

                  for (long l = 0true; l++) {
                      bb.putLong(0, l);

                      ringBuffer.publishEvent((event, sequence) -> event.set(bb.getLong(0)));

                      Thread.sleep(1000);
                  }
              }

              /**
               * <code>
               ByteBuffer bb = ByteBuffer.allocate(8);
                  for (long l = 0; true; l++)
                  {
                      bb.putLong(0, l);
                      ringBuffer.publishEvent((event, sequence) -> event.set(bb.getLong(0)));
                      Thread.sleep(1000);
                  }
                  </code> This would create a capturing lambda, meaning that it would need
               * to instantiate an object to hold the ByteBuffer bb variable as it passes
               * the lambda through to the publishEvent() call. This will create
               * additional (unnecessary) garbage, so the call that passes the argument
               * through to the lambda should be preferred if low GC pressure is a
               * requirement.Give that method references can be used instead of anonymous
               * lamdbas it is possible to rewrite the example in this fashion.
               * 
               
          */
              @SuppressWarnings("unchecked")
              private static void usingJava8Another() throws InterruptedException {

                  // Executor that will be used to construct new threads for consumers
                  Executor executor = Executors.newCachedThreadPool();

                  // Specify the size of the ring buffer, must be power of 2.
                  int bufferSize = 1024;

                  // Construct the Disruptor
                  Disruptor<LongEvent> disruptor = new Disruptor<>(LongEvent::new, bufferSize, executor);
                  // Connect the handler
                  disruptor.handleEventsWith(DisruptorExample::handleEvent);
                  // Start the Disruptor, starts all threads running
                  disruptor.start();

                  // Get the ring buffer from the Disruptor to be used for publishing.
                  RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();

                  ByteBuffer bb = ByteBuffer.allocate(8);

                  for (long l = 0true; l++) {
                      bb.putLong(0, l);

                      ringBuffer.publishEvent(DisruptorExample::translate, bb);

                      Thread.sleep(1000);
                  }
              }

              public static void handleEvent(LongEvent event, long sequence, boolean endOfBatch) {
                  System.out.println(event);
              }

              public static void translate(LongEvent event, long sequence, ByteBuffer buffer) {
                  event.set(buffer.getLong(0));
              }
          }
          posted on 2015-06-15 18:34 landon 閱讀(3947) 評論(0)  編輯  收藏 所屬分類: ProgramServerFramework
          主站蜘蛛池模板: 北海市| 化隆| 广丰县| 图片| 泗阳县| 从江县| 资阳市| 东乌珠穆沁旗| 磐安县| 北京市| 突泉县| 宣城市| 广州市| 眉山市| 凌云县| 南澳县| 万载县| 青田县| 抚顺市| 三河市| 来安县| 连云港市| 寿阳县| 晋宁县| 西峡县| 广东省| 太康县| 罗源县| 淳化县| 确山县| 昔阳县| 宜都市| 治多县| 洛阳市| 白山市| 东兰县| 黎平县| 定边县| 怀来县| 衢州市| 开平市|