当前位置: 代码网 > it编程>编程语言>Java > SpringBoot 整合线程池的示例详解

SpringBoot 整合线程池的示例详解

2024年08月14日 Java 我要评论
线程池简介什么是线程池线程池是一种利用池化技术思想来实现的线程管理技术,主要是为了复用线程、便利地管理线程和任务、并将线程的创建和任务的执行解耦开来。我们可以创建线程池来复用已经创建的线程来降低频繁创

线程池简介

什么是线程池

线程池是一种利用池化技术思想来实现的线程管理技术,主要是为了复用线程、便利地管理线程和任务、并将线程的创建和任务的执行解耦开来。我们可以创建线程池来复用已经创建的线程来降低频繁创建和销毁线程所带来的资源消耗。在java中主要是使用threadpoolexecutor类来创建线程池,并且jdk中也提供了executors工厂类来创建线程池(不推荐使用)。

线程池的优点

  • 降低资源消耗,复用已创建的线程来降低创建和销毁线程的消耗。
  • 提高响应速度,任务到达时,可以不需要等待线程的创建立即执行。
  • 提高线程的可管理性,使用线程池能够统一的分配、调优和监控。

1.启动类加 @enableasync 注解

@springbootapplication
@enableasync
public class facadeh5application {
    public static void main(string[] args) {
        springapplication.run(facadeh5application.class, args);
    }
}

2.在方法上加 @async 注解

@async
public void m1() {
    //do something
}

注意:导致 @async 注解失效的几个原因

  • 两个方法都在同一个类里面,一个方法调用另一个异步方法,不生效。但是如果在本类中注入自己的实例,再通过自己的实例调用异步方法就可行。
  • @async 方法所在的类没有交给 spring 代理(没加诸如@component注解),不生效。
  • 注解的方法不是是public方法,不生效。

3.创建线程池配置类

默认的线程池配置如下

# 核心线程数
spring.task.execution.pool.core-size=8  
# 最大线程数
spring.task.execution.pool.max-size=16
# 空闲线程存活时间
spring.task.execution.pool.keep-alive=60s
# 是否允许核心线程超时
spring.task.execution.pool.allow-core-thread-timeout=true
# 线程队列数量
spring.task.execution.pool.queue-capacity=100
# 线程关闭等待
spring.task.execution.shutdown.await-termination=false
spring.task.execution.shutdown.await-termination-period=
# 线程名称前缀
spring.task.execution.thread-name-prefix=task-

创建线程池配置类

@configuration
public class threadpoolconfig {
    @bean
    public taskexecutor taskexecutor(){
        threadpooltaskexecutor executor = new threadpooltaskexecutor();
        //设置核心线程数
        executor.setcorepoolsize(10);
        //设置最大线程数
        executor.setmaxpoolsize(20);
        //设置队列容量
        executor.setqueuecapacity(20);
        //设置线程活跃时间
        executor.setkeepaliveseconds(30);
        //设置线程名称前缀
        executor.setthreadnameprefix("sendsms-");
        //设置拒绝策略
        executor.setrejectedexecutionhandler(new threadpoolexecutor.callerrunspolicy());
        //等待所有任务结束后再关闭线程池
        executor.setwaitfortaskstocompleteonshutdown(true);
        //设置线程池中任务的等待时间
        executor.setawaitterminationseconds(60);
        return executor;
    }
}

配置多个线程池

有时候,一个项目中如果配置了多个线程池,那需要在 @bean后面加上线程池的名称

@configuration
public class threadpoolconfig {
    @bean("threadpool1")
    public taskexecutor taskexecutor(){
        threadpooltaskexecutor executor = new threadpooltaskexecutor();
		......
        return executor;
    }
    @bean("threadpool2")
    public taskexecutor taskexecutor(){
        threadpooltaskexecutor executor = new threadpooltaskexecutor();
		......
        return executor;
    }
}

在使用 @async注解时就需要指明具体使用的线程池,如下格式

@async("threadpool1")
public void m1() {
    //do something
}

到此这篇关于springboot 整合线程池的文章就介绍到这了,更多相关springboot 整合线程池内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com