并发的场景

最近在编码中遇到的场景,我的程序需要处理不同类型的任务,场景要求如下:

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并发框架的分类任务并发的更多相关文章

  1. 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 ...

  2. 并发编程之Disruptor并发框架

    一.什么是Disruptor Martin Fowler在自己网站上写了一篇LMAX架构的文章,在文章中他介绍了LMAX是一种新型零售金融交易平台,它能够以很低的延迟产生大量交易.这个系统是建立在JV ...

  3. Disruptor 并发框架

    什么是Disruptor Martin Fowler在自己网站上写了一篇LMAX架构的文章,在文章中他介绍了LMAX是一种新型零售金融交易平台,它能够以很低的延迟产生大量交易.这个系统是建立在JVM平 ...

  4. Disruptor并发框架(一)简介&上手demo

    框架简介 Martin Fowler在自己网站上写了一篇LMAX架构的文章,在文章中他介绍了LMAX是一种新型零售金融交易平台,它能够以很低的延迟产生大量交易.这个系统是建立在JVM平台上,其核心是一 ...

  5. 并发框架Disruptor译文

    Martin Fowler在自己网站上写了一篇LMAX架构的文章,在文章中他介绍了LMAX是一种新型零售金融交易平台,它能够以很低的延迟产生大量交易.这个系统是建立在JVM平台上,其核心是一个业务逻辑 ...

  6. Disruptor并发框架简介

    Martin Fowler在自己网站上写一篇LMAX架构的文章,在文章中他介绍了LMAX是一种新型零售金额交易平台,它能够以很低的延迟产生大量交易.这个系统是建立在JVM平台上,其核心是一个业务逻辑处 ...

  7. 无锁并发框架Disruptor学习入门

    刚刚听说disruptor,大概理一下,只为方便自己理解,文末是一些自己认为比较好的博文,如果有需要的同学可以参考. 本文目标:快速了解Disruptor是什么,主要概念,怎么用 1.Disrupto ...

  8. Disruptor 高性能并发框架二次封装

    Disruptor是一款java高性能无锁并发处理框架.和JDK中的BlockingQueue有相似处,但是它的处理速度非常快!!!号称“一个线程一秒钟可以处理600W个订单”(反正渣渣电脑是没体会到 ...

  9. Java 并发系列之十:java 并发框架(2个)

    1. Fork/Join框架 2. Executor框架 3. ThreadPoolExecutor 4. ScheduledThreadPoolExecutor 5. FutureTask 6. t ...

随机推荐

  1. java实验报告一

    一.实验内容 1. 使用JDK编译.运行简单的Java程序 2.使用Eclipse 编辑.编译.运行.调试Java程序 二.实验步骤 (一)命令行下Java程序开发 1. 首先双击桌面上的Xface终 ...

  2. linux 常用命令-tar(压缩、解压)

    linux中通过tar命令来压缩解压文件,常用命令如下 主选项(主选项是必须要有的,作用是告诉这次操作的主要目的): 1)c: (create)创建压缩包或者打包 2)x:(extract)拆包 3) ...

  3. Beta版本冲刺(四)

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员:恺琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示组 ...

  4. c++中队列queue和栈stack的基本操作

    1.queue 模板类的定义在<queue>头文件中. 定义queue 对象的示例代码如下:queue<int> q1;queue<double> q2; queu ...

  5. day8——ajax传参到action(Struts2)

    第一种:url+?+参数 jsp中: $(function(){ $("[name='delemp']").click(function(){ $this = $(this); $ ...

  6. clear & file input & reset & file input

    clear & file input & reset & file input Clear <input type="file"> docume ...

  7. 我为什么放弃MySQL?选择了MongoDB

    最近有个项目的功能模块,为了处理方便,需要操作集合类型的数据以及其他原因.考虑再三最终决定放弃使用MySQL,而选择MongoDB. 两个数据库,大家应该都不陌生.他们最大的区别就是MySQL为关系型 ...

  8. P2622 关灯问题II

    题目描述 现有n盏灯,以及m个按钮.每个按钮可以同时控制这n盏灯——按下了第i个按钮,对于所有的灯都有一个效果.按下i按钮对于第j盏灯,是下面3中效果之一:如果a[i][j]为1,那么当这盏灯开了的时 ...

  9. Semi synchronous replication

    目标 主库宕机不丢数据(Master Failover without data loss) facebook有两篇不错的文章: 2015/01: performance-issues-and-fix ...

  10. 【小记】FreeRTOS任务创建后但任务中为空时运行错误

    FreeRTOS任务创建后但任务中无语句为空时运行错误 会死在文件<port.c>中下边函数处 static void prvTaskExitError( void ){ /* A fun ...