`
san_yun
  • 浏览: 2652339 次
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

disruptor 入门笔记

 
阅读更多

disruptor入门例子:

package com.taobao.eagleeye.rt.storm;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import com.taobao.eagleeye.digest.handler.DigestJob;
import com.taobao.eagleeye.digest.handler.DigesterWorkerHanlder;
import com.taobao.eagleeye.disruptor.EventHandler;
import com.taobao.eagleeye.disruptor.RingBuffer;
import com.taobao.eagleeye.disruptor.dsl.Disruptor;
import com.taobao.tlog.util.NamedThreadFactory;

public class Test {

	private static final int RING_BUFFER_SIZE = 8;

	public static void main(String[] args) {
		ExecutorService es = new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                60L, TimeUnit.SECONDS,
                new SynchronousQueue<Runnable>(),new NamedThreadFactory("disruptor-process"));
		Disruptor<DigestJob> disruptor = new Disruptor<DigestJob>(DigestJob.EVENT_FACTORY, RING_BUFFER_SIZE, es);
		DigesterWorkerHanlder[] workers = new DigesterWorkerHanlder[12];
		disruptor.handleEventsWith(new EventHandler<DigestJob>() {

			@Override
			public void onEvent(DigestJob event, long sequence, boolean endOfBatch) throws Exception {
				System.out.println("event=" + event);
				Thread.sleep(10000);
			}

		});
		RingBuffer<DigestJob> ringBuffer = disruptor.start();
		while (true) {
			long seq = ringBuffer.next();
			DigestJob job = ringBuffer.get(seq);
			job.context = null;
			ringBuffer.publish(seq);
			System.out.println("emit..."+seq);
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

 

package com.taobao.tlog.runtime;

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

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.taobao.tlog.disruptor.EventFactory;
import com.taobao.tlog.disruptor.EventHandler;
import com.taobao.tlog.disruptor.RingBuffer;
import com.taobao.tlog.disruptor.WorkHandler;
import com.taobao.tlog.disruptor.dsl.Disruptor;

public class ActorTest {

	static class ActorHandler implements
			WorkHandler<String>,
			EventHandler<String> {

		@Override
		public void onEvent(String event, long sequence, boolean endOfBatch) throws Exception {
			onEvent(event);
		}

		@Override
		public void onEvent(String event) throws Exception {
			System.out.println(event);
			throw new RuntimeException("fuck");
		}

	}

	public static void main(String[] args) {
		ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat(
				"Action" + "-thread-%d").build();

		EventFactory<String> EVENT_FACTORY = new EventFactory<String>() {
			@Override
			public String newInstance() {
				return new String();
			}
		};

		ExecutorService es = Executors.newCachedThreadPool(namedThreadFactory);

		Disruptor<String> disruptor = new Disruptor<String>(EVENT_FACTORY, 8, es);

		disruptor.handleEventsWith(new ActorHandler());
		RingBuffer<String> ringBuffer = disruptor.start();
		long seq = ringBuffer.next();
		String k = ringBuffer.get(seq);
		k = "test";
		ringBuffer.publish(seq);
	}
}

 

分享到:
评论

相关推荐

    Disruptor阅读笔记.md.pdf

    Disruptor阅读笔记.md

    Disruptor并发框架中文参考文档

    ### Disruptor并发框架知识点详解 #### 一、Disruptor简介及原理 **Disruptor** 是一款高性能、低延迟的并发框架,它通过无锁设计实现了高效的队列操作,从而大大提升了多线程环境下的性能表现。该框架于2011年...

    springboot整合Disruptor并发编程框架 #资源达人分享计划#

    SpringBoot整合Disruptor并发编程框架是针对高并发场景下性能优化的一种技术实践。Disruptor是由LMAX公司开发的一款高性能、低延迟的并发工具,它通过消除线程间的锁竞争,大大提升了多线程环境下的处理速度。...

    spring-boot-starter-disruptor.zip

    《Spring Boot Starter Disruptor深度解析》 在现代软件开发中,高性能和低延迟往往是系统设计的关键要素。Spring Boot作为Java领域最受欢迎的微服务框架,提供了丰富的启动器(starters)来简化开发工作。"spring-...

    disruptor-3.4.4.jar disruptor 3.4.4 jar 官方github下载

    disruptor-3.4.4.jar 官方github下载 亲测可用,大家赶紧下载吧 后续再补充其他常用jar(但不好下载的)

    Disruptor3.x Disruptor使用方式

    Disruptor3.x Disruptor使用方式 EventHandler[] eventHandlers=new DisruptorEventHandler[]{new DisruptorEventHandler()}; DisruptorPublisher dp=new DisruptorPublisher(1024, eventHandlers); dp.start(); ...

    DisruptorDemo.zip

    《Disruptor技术详解——基于DisruptorDemo.zip实例解析》 Disruptor,由LMAX公司开发并开源,是一款高性能、低延迟的并发工具,主要用于优化多线程间的通信。它采用一种环形缓冲区(Ring Buffer)的设计,极大地...

    Disruptor demo

    Disruptor是一款高性能的并发工具库,由LMAX公司开发并开源,主要应用于高频率交易系统。它通过优化线程间通信的方式,极大地提升了多线程环境下的数据处理速度。Disruptor的设计理念是避免传统的锁机制,转而采用一...

    disruptor 多个消费者

    Disruptor是由LMAX公司开发的一种高性能的并发编程框架,主要应用于金融交易系统。它通过优化数据共享方式,显著提高了多线程环境下的处理速度。在"Disruptor 多个消费者"的场景中,我们可以深入理解Disruptor如何...

    disruptor jar包+Demo+Api

    《Disruptor 框架详解与应用实例》 Disruptor 是一款高性能的并发工具库,由英国的 LMAX 公司开发并开源。它主要用于优化多线程环境下的数据共享,尤其在金融交易系统中表现卓越。Disruptor 的核心设计是一个环形...

    disruptor-3.3.8.jar

    Error: java.lang.NoSuchMethodError: com.lmax.disruptor.dsl.Disruptor.&lt;init&gt;(Lcom/lmax/disruptor/EventFactory;ILjava/util/concurrent/ThreadFactory;Lcom/lmax/disruptor/dsl/ProducerType;Lcom/lmax/...

    Disruptor C++版(仅支持单生产者)

    Disruptor是由LMAX公司开发的一种高性能的并发编程框架,主要应用于金融交易系统。它以其高效、低延迟的事件处理机制而闻名。在C++版本的Disruptor中,我们同样可以享受到这种高效的并发能力,尤其适用于需要大量...

    disruptor-3.3.0-API文档-中文版.zip

    赠送jar包:disruptor-3.3.0.jar; 赠送原API文档:disruptor-3.3.0-javadoc.jar; 赠送源代码:disruptor-3.3.0-sources.jar; 赠送Maven依赖信息文件:disruptor-3.3.0.pom; 包含翻译后的API文档:disruptor-...

    disruptor案例加简单说明

    简单讲解disruptor并附上demo

    disruptor-3.4.2.jar 及 disruptor-3.4.2-sources.jar

    disruptor-3.4.2.jar 工具jar包 及 disruptor-3.4.2-sources.jar, Disruptor它是一个开源的并发框架,并获得2011 Duke’s 程序框架创新奖,能够在无锁的情况下实现网络的Queue并发操作,是 log4j2 引用的 jar 包

    LMAX disruptor jar包+Demo+Api+src源码 disruptor-3.0.1.jar

    LMAX Disruptor是一款高性能的消息处理框架,由LMAX公司开发并开源,它在金融交易领域有着广泛的应用。Disruptor的设计目标是解决多线程环境下的数据共享问题,通过优化并发性能,实现极低的延迟和高吞吐量。在Java...

    Disruptor应用实例

    《Disruptor应用实例》 Disruptor是高性能并发编程领域的一个重要工具,由LMAX公司开发并开源,主要用于优化多线程环境下的数据处理。它通过一种创新的数据同步方式,极大地提升了系统的吞吐量和响应速度。在本文中...

    disruptor-3.3.0-API文档-中英对照版.zip

    赠送jar包:disruptor-3.3.0.jar; 赠送原API文档:disruptor-3.3.0-javadoc.jar; 赠送源代码:disruptor-3.3.0-sources.jar; 赠送Maven依赖信息文件:disruptor-3.3.0.pom; 包含翻译后的API文档:disruptor-...

Global site tag (gtag.js) - Google Analytics