7

动态线程池使用_码农小宋的技术博客_51CTO博客

 1 year ago
source link: https://blog.51cto.com/u_15430445/5819952
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

动态线程池使用

精选 原创

码农小宋 2022-11-03 14:33:39 ©著作权

文章标签 spring 线程池 xml 文章分类 Java 编程语言 yyds干货盘点 阅读数1.5万

1、复制到项目lib下

动态线程池使用_spring

2、File->Project Structure... ->Modeles  ->Dependencies

动态线程池使用_xml_02
动态线程池使用_线程池_03

找到目录下的jar包引用进去

3、配置pom.xml

<!--动态线程池配置 begin-->
<dependency>
<groupId>com.spz</groupId>
<artifactId>dtp-spring-boot-starter</artifactId>
<version>0.0.1</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/dtp-spring-boot-starter.jar</systemPath>
</dependency>

<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<version>0.2.7</version>
</dependency>
<!--动态线程池配置 end-->

这个支持nacos动态更改内置nacos的监听事件所以引用了nacos  也可也在yaml里使用

若使用nacos请配置dtp.yaml文件

#nacos配置
nacos:
config:
server-addr: 127.0.0.1:8848
data-id: dtp.yaml
type: yaml
auto-refresh: true
bootstrap:
enable: true

dtp.yaml的配置如下

#动态配置
dtp:
executors:
- name: t1
core-pool-size: 10
maximum-pool-Size: 100
- name: t2
core-pool-size: 20
maximum-pool-Size: 110

name为使用线程池的名称 使用案例如下

@GetMapping("/test1")
public Integer test1() {
DtpExecutor t1 = DtpUtil.get("t1");

t1.execute(() -> doTask());

return t1.getCorePoolSize();
}

@GetMapping("/test2")
public Integer test2() {
DtpExecutor t2 = DtpUtil.get("t2");

t2.execute(() -> doTask());

return t2.getCorePoolSize();
}

所用jar包

 ​https://pan.xunlei.com/s/VNFwDN8GhAarxCV8wzxQv7EeA1?pwd=38cc#​​​

提取码:38cc 

  • 收藏
  • 评论
  • 分享
  • 举报

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK