• 大小: 581KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-14
  • 语言: Java
  • 标签: disruptor  

资源简介

disruptor 的jar包和api 自带了一个简单的例子。带注释 不是官方的例子。 不过是转别人的。 看了之后很容易明白。

资源截图

代码片段和文件信息

package demo.test;


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.junit.Before;
import org.junit.Test;

import com.lmax.disruptor.BatchEventProcessor;
import com.lmax.disruptor.BlockingWaitStrategy;
import com.lmax.disruptor.EventHandler;
import com.lmax.disruptor.MultiThreadedClaimStrategy;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.SequenceBarrier;
import com.lmax.disruptor.SingleThreadedClaimStrategy;
import com.lmax.disruptor.YieldingWaitStrategy;
import com.lmax.disruptor.dsl.Disruptor;

public class DisruptorConceptProofTest {

private long _journalistCount = 0L;
// 用来验证日志线程看到的最后一个事件值
private int _lastEventValue = 0;
// 用来验证日志线程看到了所有生产线程产生的事件
private int _journalistValueSum = 0;
// 将事件保存在硬盘里的书记员线程
final EventHandler _journalist = new EventHandler() {
public void onEvent(final TicketEvent event final long sequence
final boolean endOfBatch) throws Exception {
_journalistCount++;
_lastEventValue = event.getValue();
_journalistValueSum += _lastEventValue;
}
};

private long _replicatorCount = 0L;
// 用来验证备份线程看到了所有生产线程产生的事件
private int _replicatorValueSum = 0;
// 将事件发送到备份服务器保存的备份线程
final EventHandler _replicator = new EventHandler() {
public void onEvent(final TicketEvent event final long sequence
final boolean endOfBatch) throws Exception {
_replicatorCount++;
_replicatorValueSum += event.getValue();
}
};

final EventHandler _eventProcessor = new EventHandler() {
public void onEvent(final TicketEvent event final long sequence
final boolean endOfBatch) throws Exception {
System.out.println(“[processor] ====“
+ Thread.currentThread().getName() + “===>“
+ sequence);
}
};

private int RING_SIZE = 128;
private final ExecutorService EXECUTOR = Executors.newCachedThreadPool();

@Before
public void setUp() throws Exception {
_journalistCount = _replicatorCount = _lastEventValue = 0;
}

@Test
public void demo() throws Exception {
演示disruptor的基本用法();
}


private void 演示disruptor的基本用法() throws Exception {
RingBuffer ringBuffer = new RingBuffer
TicketPoolService.INSTANCE new MultiThreadedClaimStrategy(
RING_SIZE) new YieldingWaitStrategy());

SequenceBarrier barrier = ringBuffer.newBarrier();
System.err.println(Thread.currentThread().getName());
// // 注册日志线程
// BatchEventProcessor journalist = new BatchEventProcessor
// ringBuffer barrier _journalist);
// ringBuffer.setGatingSequences(journalist.getSequence());
// EXECUTOR.submit(journalist);
//
// // 注册备份线程
// BatchEventProcessor replicator = new BatchEventProcessor
// ringBuffer barrier _replicator);
// ringBuffer.setGatingSequences(replicator.getSequence());
// EXECUTOR.

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件        5553  2013-04-10 09:51  DisruptorConceptProofTest.java
     文件         344  2013-04-10 09:22  TicketEvent.java
     文件          72  2013-04-10 09:23  TicketEventType.java
     文件         354  2013-04-10 09:23  TicketPoolService.java
     目录           0  2013-04-09 18:21  api\
     目录           0  2012-08-22 06:51  api\meta-INF\
     文件         106  2012-08-22 06:51  api\meta-INF\MANIFEST.MF
     文件        6464  2012-08-22 06:51  api\allclasses-frame.html
     文件        5624  2012-08-22 06:51  api\allclasses-noframe.html
     目录           0  2012-08-22 06:51  api\com\
     目录           0  2012-08-22 06:51  api\com\lmax\
     目录           0  2012-08-22 06:51  api\com\lmax\disruptor\
     文件       23192  2012-08-22 06:51  api\com\lmax\disruptor\AbstractMultithreadedClaimStrategy.html
     文件       15975  2012-08-22 06:51  api\com\lmax\disruptor\AggregateEventHandler.html
     文件       11771  2012-08-22 06:51  api\com\lmax\disruptor\AlertException.html
     文件       10038  2012-08-22 06:51  api\com\lmax\disruptor\BatchDescriptor.html
     文件       18734  2012-08-22 06:51  api\com\lmax\disruptor\BatchEventProcessor.html
     文件       17919  2012-08-22 06:51  api\com\lmax\disruptor\BlockingWaitStrategy.html
     文件       17964  2012-08-22 06:51  api\com\lmax\disruptor\BusySpinWaitStrategy.html
     文件       18375  2012-08-22 06:51  api\com\lmax\disruptor\ClaimStrategy.html
     文件        8528  2012-08-22 06:51  api\com\lmax\disruptor\EventFactory.html
     文件       10389  2012-08-22 06:51  api\com\lmax\disruptor\EventHandler.html
     文件       11037  2012-08-22 06:51  api\com\lmax\disruptor\EventProcessor.html
     文件       13223  2012-08-22 06:51  api\com\lmax\disruptor\EventPublisher.html
     文件        9029  2012-08-22 06:51  api\com\lmax\disruptor\EventTranslator.html
     文件       11700  2012-08-22 06:51  api\com\lmax\disruptor\ExceptionHandler.html
     文件       15749  2012-08-22 06:51  api\com\lmax\disruptor\FatalExceptionHandler.html
     文件       15730  2012-08-22 06:51  api\com\lmax\disruptor\IgnoreExceptionHandler.html
     文件       11340  2012-08-22 06:51  api\com\lmax\disruptor\InsufficientCapacityException.html
     文件        9107  2012-08-22 06:51  api\com\lmax\disruptor\LifecycleAware.html
     文件       17268  2012-08-22 06:51  api\com\lmax\disruptor\MultiThreadedClaimStrategy.html
............此处省略104个文件信息

评论

共有 条评论

相关资源