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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/218623.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

HarmonyOS4.0从零开始的开发教程12给您的应用添加弹窗

HarmonyOS&#xff08;十&#xff09;给您的应用添加弹窗 概述 在我们日常使用应用的时候&#xff0c;可能会进行一些敏感的操作&#xff0c;比如删除联系人&#xff0c;这时候我们给应用添加弹窗来提示用户是否需要执行该操作&#xff0c;如下图所示&#xff1a; 弹窗是一种…

工作招聘

https://jobs.bilibili.com/social/positions/16978 Java开发工程师&#xff08;交易业务&#xff09; 上海全职技术类2023-11-15 发布 职位描述 工作职责: 1、B站电商平台架构整体设计&#xff0c;B站电商业务高速发展背景下&#xff0c;电商业务中台建设&#xff0c;如商品…

AI全栈大模型工程师(二十八)如何做好算法备案

互联网信息服务算法 什么情况下要备案&#xff1f; 对于B2B业务&#xff0c;不需要备案。 但在B2C领域&#xff0c;一切要视具体情况而定。 如果我们自主训练大型模型&#xff0c;这是必要的。 但如果是基于第三方模型提供的服务&#xff0c;建议选择那些已获得备案并且具有较大…

前端小记--4.前端终止jquery的ajax请求

在项目中&#xff0c;有时需要主动终止数据请求&#xff0c;比如&#xff0c;某个数据请求响应时间比较慢&#xff0c;用户在等待请求的过程中取消请求&#xff0c;但后台服务器仍在执行计算&#xff0c;此时就需要前端主动终止请求&#xff0c;避免资源浪费。 这里以终止jque…

前端打包工具之Webpack5

前端打包工具之Webpack5 什么是打包工具打包工具的作用常见的打包工具一、Webpack1、什么是[webpack](https://webpack.docschina.org/concepts/)2、webpack本身的功能是有限的3、webpack基本使用&#xff1a;基于本身功能只解析JS资源4、webpack配置4.1、entry&#xff08;入口…

Python脚本打包

一.Windows操作系统 Python脚本打包 1.cmd窗口执行如下指令&#xff1a;pip install pyinstaller C:\Users\ZhuQing>pip install pyinstaller2.执行以下指令验证pyinstaller是否安装成功&#xff08;pyinstaller&#xff09; C:\Users\ZhuQing>pyinstaller3.被打包程序…

frp配置内网穿透步骤

frp配置内网穿透步骤 1.环境准备1.1 云服务器1.2 frp包 2. frp安装2.1 server服务端设置2.2 客户端配置 实现目标通过云服务器ip:8080访问内网电脑启动的web项目localhost:8080 1.环境准备 1.1 云服务器 服务器安装centos7.9, 安全组入口方向开通 7500 7000 8080 8060端口 …

DPDK是什么?DPDK网卡更有优势吗?

近年来&#xff0c;随着数字化的推进&#xff0c;上云成为企业数字化建设的重要指标&#xff0c;用云程度持续深入。可以说&#xff0c;云时代已经来临。 应云而生的DPDK 云时代的一个典型特征&#xff0c;是数据的高速增长。据华为GIV数据&#xff0c;预计2025年全球数据量将…

Spark读写Hive

Spark读写Hive 文章目录 Spark读写Hive&#xff08;一&#xff09;配置本地域名映射&#xff08;二&#xff09;创建Hive表&#xff08;三&#xff09;IDEA中编写Spark代码读取Hive数据&#xff08;四&#xff09;IDEA中编写Spark代码写入数据到Hive &#xff08;一&#xff09…

前端优化 ----防抖 节流

防抖 如果一个事件在短时间内连续触发&#xff0c;则只去执行最后一次。 控制频率 实现方式&#xff1a;每次触发事件时设置一个延迟调用方法&#xff0c;并且取消之前的延时调用方法缺点&#xff1a;如果事件在规定的时间间隔内被不断的触发&#xff0c;则调用方法会被不断的…

【力扣】2.两数相加

2.两数相加 这是第二题&#xff0c;还行豁~。 题解&#xff1a; 首先就是对题目的理解。这里你要知道两链表中数字的排列都是逆序的&#xff0c;也就是说示例一中2-4-3他原本的数字应该是342。同理可得下面链表的意思&#xff0c;二者相加所得到的结果也是逆序的&#xff0c;…

企业微信群裂变:重塑新时代营销格局

随着数字化时代的来临&#xff0c;社交媒体已经逐渐成为人们获取和交流信息的主要平台。微信&#xff0c;作为中国最大的社交平台&#xff0c;正在上演着一场名为“微信群裂变”的营销革命。本文将深入剖析企业微信群裂变的内涵&#xff0c;揭示其背后的驱动力&#xff0c;以及…

【Java】MySQL存储 MD5 值应该用 VARCHAR 还是CHAR?

CHAR 非常适合存储 MD5 值。因为MD5 值是一个定长的值&#xff0c;对于固定长度的非常短的列&#xff0c;CHAR比VARCHAR的效率更高。 CHAR和VARCHAR的区别: 1)存储长度不同&#xff0c;CHAR 的长度是固定的&#xff0c;VARCHAR 的长度是可变的 假设&#xff0c;当我们定义 CHA…

基于Java8构建Docke镜像

基于Java8构建Docke镜像 搜索java8安装包 docker search java8 --no-trunc &#xff0c; --no-trunc展开描述信息 选择拉取 docker pull docker.io/mykro/java8-jre&#xff0c;为了减少磁盘占用&#xff0c;选择jre版本基础镜像 在宿主机创建文件夹iot&#xff0c;并把所需…

Reactor模型

目录 1.Reactor模型是什么2.Reactor 模型应用场景3.使用 Reactor 模型的软件4.Reactor 模型 与 Actor 模型 的关系 本文主要介绍Reactor模型基本概念以及应用场景。 1.Reactor模型是什么 Reactor模型是一种事件驱动的设计模式&#xff0c;用于处理服务请求&#xff0c;它是由…

第15节:Vue3 DOM 更新完成nextTick()

下面是一个示例&#xff0c;演示了如何在UniApp中使用Vue3框架使用nextTick()&#xff1a; <template> <view> <button click"changeText">点击改变文本</button> <text>{{ message }}</text> </view> </templa…

python笔记(1)安装环境

1&#xff0c;官网下载自己电脑位数的安装包 https://www.python.org/downloads/windows/ install时勾选中add to path&#xff0c;把路径自动添加到环境变量 安装pycharm就不讲了 安装后选中自己的python安装包 file-> setting->project:yourprojectname ->pyt…

FastAPI之请求头

请求头 FastAPI是一个现代、快速&#xff08;高性能&#xff09;、异步的Python Web框架&#xff0c;用于构建RESTful APIs。它基于标准的Python类型提示&#xff0c;并且可以生成交互式的API文档。 在本教程中&#xff0c;我们将学习如何使用FastAPI处理请求头&#xff08;H…

【详解优先级队列(堆)】

目录 堆的概念 堆的性质 堆的存储方式 堆的创建 堆的向下调整 向下过程(以小堆为例) 向下过程(以大堆为例) 建堆的时间复杂度O(n) 堆的插入与删除 堆的插入 向上调整建堆的时间复杂度O(nlogn) 堆的删除 常见习题 常用接口介绍 PriorityQueue的特性 Pri…

成功的云转型之路需要考虑的基本因素

云计算如今已经变得无处不在&#xff0c;并显著影响着日常生活的各个方面。然而&#xff0c;重要的是要注意云计算技术是不断发展的。最近向远程工作的转变促使企业加快数字化转型&#xff0c;更多地采用云计算服务。 即使在新冠疫情消退之后&#xff0c;云计算技术的采用也获得…