基于Disruptor并发框架的分类任务并发
并发的场景
最近在编码中遇到的场景,我的程序需要处理不同类型的任务,场景要求如下:
1.同类任务串行、不同类任务并发。
2.高吞吐量。
3.任务类型动态增减。
思路
思路一:
最直接的想法,每有一个任务种类被新建,就创建对应的处理线程。
这样的思路问题在于线程数量不可控、创建、销毁线程开销大。不可取。
思路二:
比较常规的想法,所有任务共享线程池每有一个任务种类被创建,就新建一个队列,以保证同类任务串行。
这样的思路问题在于数据结构开销不可控,如果是任务种类繁多,但每种任务数量并不多的情况,那么建如此多的队列显得可笑。
于是我期望能够使用一个线程池、一个队列搞定这些事。
设计
代码实现
引入disruptor依赖:
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.4.2</version>
</dependency>
Task接口
public interface Task<T> {
public T exec();
}
TaskEvent类
import com.lmax.disruptor.EventFactory;
public class TaskEvent<T> {
private Task<T> input;
//用于标记一个并发分组
private int partitionId;
//disruptor的任务事件工场
public static final EventFactory<TaskEvent> FACTORY = TaskEvent::new;
public Task<T> getInput() {
return input;
}
public void setInput(Task<T> input) {
this.input = input;
}
public int getPartitionId() {
return partitionId;
}
public void setPartitionId(int partitionId) {
this.partitionId = partitionId;
}
}
TaskHandler类
import com.lmax.disruptor.EventHandler;
import com.lmax.disruptor.LifecycleAware;
public class EventAdaptor<T> implements EventHandler<TaskEvent<T>>, LifecycleAware {
//决定我处理那些任务
private final int partitionId;
public EventAdaptor(int partitionId) {
super();
this.partitionId = partitionId;
}
public void onEvent(TaskEvent<T> taskEvent, long arg1, boolean arg2) throws Exception {
if(partitionId == taskEvent.getPartitionId()) {
taskEvent.getInput().exec();
}
}
@Override
public void onShutdown() {
Thread.currentThread().setName("handler-" + partitionId);
}
@Override
public void onStart() {
Thread.currentThread().setName("handler-" + partitionId);
}
}
TaskService类
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import javax.annotation.PostConstruct;
import com.lmax.disruptor.BlockingWaitStrategy;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;
@Service
@Singleton
public class TaskService<V extends Task<T>, T> {
private static final int BUFFER_SIZE = 1024;
private static final int DEFAULT_POOL_SIZE = 5;
private ThreadPoolExecutor executor;
private Disruptor<TaskEvent> disruptor;
private Map<String,Integer> taskClassMapperPartition = new ConcurrentHashMap<>();
private static final int PARALLEL_NUM = 5;
private List<String> taskTypes = new ArrayList<>();
@PostConstruct
public void init() {
//初始化处理器和线程池
this.executor = new ThreadPoolExecutor(DEFAULT_POOL_SIZE, DEFAULT_POOL_SIZE, 15L, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
this.executor.prestartAllCoreThreads();
this.disruptor = new Disruptor<>(TaskEvent.FACTORY, BUFFER_SIZE, executor, ProducerType.SINGLE, new BlockingWaitStrategy());
EventAdaptor[] handlers = new EventAdaptor[PARALLEL_NUM];
for(int i = 0; i < PARALLEL_NUM; i++) {
handlers[i] = new EventAdaptor(i);
}
this.disruptor.handleEventsWith(handlers);
this.disruptor.start();
rePartition();
}
public void addTaskType(String type) {
taskClassMapperPartition.put(type, taskTypes.size() % PARALLEL_NUM);
taskTypes.add(type);
}
public void deleteTaskType(String type) {
taskTypes.remove(type);
taskClassMapperPartition.remove(type);
rePartition();
}
private void rePartition() {
for(int i = 0, length = taskTypes.size(); i < length; i++) {
//给各任务处理器均衡发放任务
taskClassMapperPartition.put(taskTypes.get(i), i % PARALLEL_NUM);
}
}
}
这里给出的代码是一套物业无得简单框架,你只要实现Task接口就可以了。
基于Disruptor并发框架的分类任务并发的更多相关文章
- J.U.C并发框架
转载:http://itindex.net/detail/48869-j.u.c-%E6%A1%86%E6%9E%B6 J.U.C并发框架 作者:Doug Lea SUNY Oswego Oswego ...
- 并发编程之Disruptor并发框架
一.什么是Disruptor Martin Fowler在自己网站上写了一篇LMAX架构的文章,在文章中他介绍了LMAX是一种新型零售金融交易平台,它能够以很低的延迟产生大量交易.这个系统是建立在JV ...
- Disruptor 并发框架
什么是Disruptor Martin Fowler在自己网站上写了一篇LMAX架构的文章,在文章中他介绍了LMAX是一种新型零售金融交易平台,它能够以很低的延迟产生大量交易.这个系统是建立在JVM平 ...
- Disruptor并发框架(一)简介&上手demo
框架简介 Martin Fowler在自己网站上写了一篇LMAX架构的文章,在文章中他介绍了LMAX是一种新型零售金融交易平台,它能够以很低的延迟产生大量交易.这个系统是建立在JVM平台上,其核心是一 ...
- 并发框架Disruptor译文
Martin Fowler在自己网站上写了一篇LMAX架构的文章,在文章中他介绍了LMAX是一种新型零售金融交易平台,它能够以很低的延迟产生大量交易.这个系统是建立在JVM平台上,其核心是一个业务逻辑 ...
- Disruptor并发框架简介
Martin Fowler在自己网站上写一篇LMAX架构的文章,在文章中他介绍了LMAX是一种新型零售金额交易平台,它能够以很低的延迟产生大量交易.这个系统是建立在JVM平台上,其核心是一个业务逻辑处 ...
- 无锁并发框架Disruptor学习入门
刚刚听说disruptor,大概理一下,只为方便自己理解,文末是一些自己认为比较好的博文,如果有需要的同学可以参考. 本文目标:快速了解Disruptor是什么,主要概念,怎么用 1.Disrupto ...
- Disruptor 高性能并发框架二次封装
Disruptor是一款java高性能无锁并发处理框架.和JDK中的BlockingQueue有相似处,但是它的处理速度非常快!!!号称“一个线程一秒钟可以处理600W个订单”(反正渣渣电脑是没体会到 ...
- Java 并发系列之十:java 并发框架(2个)
1. Fork/Join框架 2. Executor框架 3. ThreadPoolExecutor 4. ScheduledThreadPoolExecutor 5. FutureTask 6. t ...
随机推荐
- 小组成员的github地址
袁颖https://github.com/joanyy/test 魏晓 https://github.com/weixiaohaobaobao/test 张晓磊 https://github.com/ ...
- DeepID1,DeepID2
1.DeepID1 (Deep Learning Face Representation from Predicting 10,000 Classes) Step1:构建网络框架 DeepConvNe ...
- Beta阶段敏捷冲刺②
1.提供当天站立式会议照片一张. 每个人的工作 (有work item 的ID),并将其记录在码云项目管理中: 1.1昨天已完成的工作. 姓名 昨天已完成的工作 徐璐琳 完成设置界面的排版 祁泽文 实 ...
- 在laravel中,使用DB查询数据库后,返回的对象,可以用下面的办法变为数组
$nodes = Db::table('account')->orderBy('sort', 'asc')->orderBy('id' ,'asc')->get()->map( ...
- python学习:python的星号(*)和双星号(**)用法
原帖地址见:[Python]-12-星号变量的特殊用法 在Python中,星号除了用于乘法数值运算和幂运算外,还有一种特殊的用法"在变量前添加单个星号或两个星号",实现多参数的传入 ...
- Chemical table CodeForces - 1012B
题意: 一个棋盘 对于任何一个棋盘中的矩形 如果 任意三角存在棋子 则第四个角会自动生成一个棋子 求铺满整个棋盘 我们至少要向棋盘里加多少枚棋子 解析: 这题就是求图中有多少个连通图,可以直接dfs ...
- bzoj 1798: [Ahoi2009]Seq 维护序列seq (线段树 ,多重标记下放)
1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec Memory Limit: 64 MBSubmit: 7773 Solved: 2792[Submit ...
- 【bzoj2434】 Noi2011—阿狸的打字机
http://www.lydsy.com/JudgeOnline/problem.php?id=2434 (题目链接) 题意 给出一个字符串,$P$表示输出,$B$表示退格.$m$组询问$(x,y)$ ...
- 8.30 牛客OI赛制测试赛1 F题 子序列
题目描述 给出一个长度为n的序列,你需要计算出所有长度为k的子序列中,除最大最小数之外所有数的乘积相乘的结果 输入描述: 第一行一个整数T,表示数据组数.对于每组数据,第一行两个整数N,k,含义如题所 ...
- ueditor的上传文件漏洞(c#)
项目中使用了ueditor,安全测试发现一个漏洞,涉及漏洞的文件名字为UploadHandler.cs,其中有一个方法: private bool CheckFileType(string filen ...