当前位置: 首页 > news >正文

Thread-Per-Message设计模式

    Thread-Per-Message是为每一个消息的处理开辟一个线程,以并发方式处理,提高系统整体的吞吐量。这种模式再日常开发中非常常见,为了避免线程的频繁创建和销毁,可以使用线程池来代替。

示例代码如下:

public class Request {
private String business;public Request(String business) {
this.business=business;
}
public String toString() {
return this.business;
}
}
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;public class TaskHandler implements Runnable{
private Request request;public TaskHandler(Request request) {
this.request=request;
}private void slowly() {
try {
TimeUnit.SECONDS.sleep(ThreadLocalRandom.current().nextInt(10));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}@Override
public void run() {
System.out.println("Begin handle "+request);
slowly();
System.out.println("End handle "+request);
}}
import org.multithread.threadpool.BaseThreadPool;
import org.multithread.threadpool.ThreadPool;public class Operator {
private final ThreadPool threadPool=new BaseThreadPool(2,6,4,100);public void call(String business) {
TaskHandler taskHandler=new TaskHandler(new Request(business));
threadPool.execute(taskHandler);
}}
public class TPMtest {
public static void main(String[] args) {
Operator op=new Operator();
String root="Root-Request-";
for(int i=0;i<30;i++) {
op.call(root+i);
}
}
}

运行结果:

Begin handle Root-Request-0
Begin handle Root-Request-1
End handle Root-Request-1
Begin handle Root-Request-2
End handle Root-Request-0
Begin handle Root-Request-3
End handle Root-Request-3
Begin handle Root-Request-4
End handle Root-Request-2
Begin handle Root-Request-5
Begin handle Root-Request-6
Begin handle Root-Request-7
End handle Root-Request-7
Begin handle Root-Request-8
End handle Root-Request-4
Begin handle Root-Request-9
End handle Root-Request-6
Begin handle Root-Request-10
End handle Root-Request-5
Begin handle Root-Request-11
End handle Root-Request-8
Begin handle Root-Request-12
Begin handle Root-Request-13
End handle Root-Request-10
Begin handle Root-Request-14
End handle Root-Request-9
Begin handle Root-Request-15
End handle Root-Request-11
Begin handle Root-Request-16
End handle Root-Request-15
Begin handle Root-Request-17
End handle Root-Request-14
Begin handle Root-Request-18
End handle Root-Request-18
Begin handle Root-Request-19
End handle Root-Request-12
Begin handle Root-Request-20
End handle Root-Request-20
Begin handle Root-Request-21
End handle Root-Request-16
Begin handle Root-Request-22
End handle Root-Request-22
Begin handle Root-Request-23
End handle Root-Request-23
Begin handle Root-Request-24
End handle Root-Request-13
Begin handle Root-Request-25
End handle Root-Request-19
Begin handle Root-Request-26
End handle Root-Request-26
Begin handle Root-Request-27
End handle Root-Request-27
Begin handle Root-Request-28
Begin handle Root-Request-29
End handle Root-Request-25
End handle Root-Request-17
End handle Root-Request-21
End handle Root-Request-24
End handle Root-Request-28
End handle Root-Request-29

相关文章:

  • 流程控制之循环
  • 解读Amazon Q | 用AI聊天机器人连接你与未来的无限可能
  • Java监听器与观察者模式
  • 如何不用手机号获取自己的opeai中的key
  • ROS2 学习08 导航Nav2:简介、安装、测试效果、错误处理
  • ​ 无限可能性的探索:Amazon Lightsail轻量应用服务器引领数字化时代创新发展
  • JAVA 版多商家入驻 直播带货 商城系统 B2B2C 之 鸿鹄云商B2B2C产品概述
  • 我的网站服务器被入侵了该怎么办?
  • Word Excel模版引擎
  • 智能部署之巅:Amazon SageMaker引领机器学习革新
  • 【FPGA】Quartus18.1打包封装网表文件(.qxp)详细教程
  • 仪器器材企业网站搭建的作用是什么
  • 测试用例设计方法六脉神剑——第四剑:石破天惊,功能图法攻阵
  • 智能优化算法应用:基于鸽群算法3D无线传感器网络(WSN)覆盖优化 - 附代码
  • 自定义Axure元件库及原型图泳道图的绘制(详细不同类的案例)
  • [译]Python中的类属性与实例属性的区别
  • Angular js 常用指令ng-if、ng-class、ng-option、ng-value、ng-click是如何使用的?
  • Angular2开发踩坑系列-生产环境编译
  •  D - 粉碎叛乱F - 其他起义
  • Java 实战开发之spring、logback配置及chrome开发神器(六)
  • Java比较器对数组,集合排序
  • JDK 6和JDK 7中的substring()方法
  • JS学习笔记——闭包
  • Spark学习笔记之相关记录
  • springMvc学习笔记(2)
  • Vue ES6 Jade Scss Webpack Gulp
  • 湖南卫视:中国白领因网络偷菜成当代最寂寞的人?
  • 基于Volley网络库实现加载多种网络图片(包括GIF动态图片、圆形图片、普通图片)...
  • 聊聊flink的TableFactory
  • 聊聊hikari连接池的leakDetectionThreshold
  • 批量截取pdf文件
  • 深度学习中的信息论知识详解
  • 什么是Javascript函数节流?
  • 怎样选择前端框架
  • mysql面试题分组并合并列
  • raise 与 raise ... from 的区别
  • 大数据全解:定义、价值及挑战
  • 机器人开始自主学习,是人类福祉,还是定时炸弹? ...
  • ​ 全球云科技基础设施:亚马逊云科技的海外服务器网络如何演进
  • ​ssh-keyscan命令--Linux命令应用大词典729个命令解读
  • # 20155222 2016-2017-2 《Java程序设计》第5周学习总结
  • #Linux(权限管理)
  • #Z2294. 打印树的直径
  • #设计模式#4.6 Flyweight(享元) 对象结构型模式
  • (30)数组元素和与数字和的绝对差
  • (C++20) consteval立即函数
  • (Redis使用系列) Springboot 使用redis实现接口Api限流 十
  • (附源码)springboot家庭财务分析系统 毕业设计641323
  • (四)Controller接口控制器详解(三)
  • (译)计算距离、方位和更多经纬度之间的点
  • (原創) 如何動態建立二維陣列(多維陣列)? (.NET) (C#)
  • (原創) 是否该学PetShop将Model和BLL分开? (.NET) (N-Tier) (PetShop) (OO)
  • (转)MVC3 类型“System.Web.Mvc.ModelClientValidationRule”同时存在
  • .NET 6 Mysql Canal (CDC 增量同步,捕获变更数据) 案例版
  • .NET Core 2.1路线图