可以放在application.propertes文件种也可以放在自己新建的config/文件目录下,注意:但是需要使用@propertysource把配置文件进行加载。
# 异步线程配置# 配置核心线程数async.executor.thread.core_pool_size = 8# 配置最大线程数async.executor.thread.max_pool_size = 20# 配置队列大小async.executor.thread.queue_capacity = 99999# 配置线程池中的线程的名称前缀async.executor.thread.name.prefix = async-rvice-
用来定义如何创建一个threadpooltaskexecutor,要使用@configuration和@enableasync这两个注解,表示这是个配置类,并且是线程池的配置类
@slf4j@enableasync@configurationpublic class rcexecutorconfig { @value("${async.executor.thread.core_pool_size}") private int corepoolsize; @value("${async.executor.thread.max_pool_size}") private int maxpoolsize; @value("${async.executor.thread.queue_capacity}") private int queuecapacity; @value("${async.executor.thread.name.prefix}") private string nameprefix; @bean(name = "asyncrviceexecutor") public executor asyncrviceexecutor() { log.info("start asyncrviceexecutor"); threadpooltaskexecutor executor = new threadpooltaskexecutor(); //threadpooltaskexecutor executor = new visiablethreadpooltaskexecutor(); //配置核心线程数 executor.tcorepoolsize(corepoolsize); //配置最大线程数 executor.tmaxpoolsize(maxpoolsize); //配置队列大小 executor.tqueuecapacity(queuecapacity); //配置线程池中的线程的名称前缀 executor.tthreadnameprefix(nameprefix); // rejection-policy:当pool已经达到max size的时候,如何处理新任务 // caller_runs:不在新线程中执行任务,而是有调用者所在的线程来执行 executor.trejectedexecutionhandler(n天津城建大学怎么样ew threadpoolexecutor.callerrunspolicy()); //执行初始化 executor.initialize(); return executor; }}
是异步线程的接口,便于测试
public interface asyncrvice { /** * 执行异步任务 * 可以根据需求,自己加参数拟定 */ void executeasync();}
将rvice层的服务异步化,在executeasync()方法上增加注解@async(“asyncrviceexecutor”),asyncrviceexecutor方法是前面rcexecutorconfig.java中的方法名,表明executeasync方法进入的线程池是asyncrviceexecutor方法创建的。
测试方面这里我加入了一个定时任务,使用的是corn表达式。(不懂得同学可以网上了解一下)
@slf4j@rvicepublic class asyncrviceimpl implements asyncrvice { @override @scheduled(cron = " */1 * * * * ? ") @async("asyncrviceexecutor") public void executeasync() { log.info("start executeasync"); system.out.println("异步线程执行批量插入等耗时任务"); log.info("end executeasync"); } }
10:32:15.004 [async-rvice-1] info c.a.a.rvice.impl.asyncrviceimpl – start executeasync
异步线程执行批量插入等耗时任务
10:32:15.004 [async-rvice-1] info c.a.a.rvice.impl.asyncrviceimpl – end executeasync
10:32:16.003 [async-rvice-2] info c.a.a.rvice.impl.asyncrviceimpl – start executeasync
异步线程执行批量插入等耗时任务
10:32:16.004 [async-rvice-2] info c.a.a.rvice.impl.asyncrviceimpl – end executeasync
10:32:17.001 [async-rvice-3] info c.a.a.rvice.impl.asyncrviceimpl – start executeasync
异步线程执行批量插入等耗时任务
10:32:17.001 [async-rvice-3] info c.a.a.rvice.impl.asyncrviceimpl – end executeasync
10:32:18.002 [async-rvice-4] info c.a.a.rvice.impl.asyncrviceimpl – start executeasync
异步线程执行批量插入等耗时任务
10:32:18.003 [async-rvice-4] info c.a.a.rvice.impl.asyncrviceimpl – end executeasync
10:32:19.002 [async-rvice-5] info c.a.a.rvice.impl.asyncrviceimpl – start executeasync
异步线程执行批量插入等耗时任务
10:32:19.003 [async-rvice-5] info c.a.a.rvice.impl.asyncrviceimpl – end executeasync
10:32:20.001 [async-rvice-6] info c.a.a.rvice.impl.asyncrviceimpl – start executeasync
异步线程执行批量插入等耗时任务
10:32:20.002 [async-rvice-6] info c.a.a.rvice.impl.asyncrviceimpl – end executeasync
10:32:21.002 [async-rvice-7] info c.a.a.rvice.impl.asyncrviceimpl – start executeasync
异步线程执行批量插入等耗时任务
10:32:21.002 [async-rvice-7] info c.a.a.rvice.impl.asyncrviceimpl – end executeasync
10:32:22.004 [async-rvice-8] info c.a.a.rvice.impl.asyncrviceimpl – start executeasync
异步线程执行批量插入等耗时任务
10:32:22.005 [async-rvice-8] info c.a.a.rvice.impl.asyncrviceimpl – end executeasync
10:32:23.001 [async-rvice-1] info c.a.a.rvice.impl.asyncrviceimpl – start executeasync
异步线程执行批量插入等耗时任务
10:32:23.003 [async-rvice-1] info c.a.a.rvice.impl.asyncrviceimpl – end executeasync
10:32:24.003 [async-rvice-2] info c.a.a.rvice.impl.asyncrviceimpl – start executeasync
异步线程执行批量插入等耗时任务
10:32:24.003 [async-rvice-2] info c.a.a.rvice.impl.asyncrviceimpl – end executeasync
10:32:25.001 [async-rvice-3] info c.a.a.rvice.impl.asyncrviceimpl – start executeasync
异步线程执行批量插入等耗时任务
10:32:25.001 [async-rvice-3] info c.a.a.rvice.impl.asyncrviceimpl – end executeasync
10:32:26.002 [async-rvice-4] info c.a.a.rvice.impl.asyncrviceimpl – start executeasync
异步线程执行批量插入等耗时任务
10:32:26.002 [async-rvice-4] info c.a.a.rvice.impl.asyncrviceimpl – end executeasync
10:32:27.002 [async-rvice-5] info c.a.a.rvice.impl.asyncrviceimpl – start executeasync
异步线程执行批量插入等耗时任务
10:32:27.003 [async-rvice-5] info c.a.a.rvice.impl.asyncrviceimpl – end executeasync
10:32:28.001 [async-rvice-6] info c.a.a.rvice.impl.asyncrviceimpl – start executeasync
异步线程执行批量插入等耗时任务
10:32:28.001 [async-rvice-6] info c.a.a.rvice.impl.asyncrviceimpl – end executeasync
10:32:29.001 [async-rvice-7] info c.a.a.rvice.impl.asyncrviceimpl – start executeasync
异步线程执行批量插入等耗时任务
10:32:29.002 [async-rvice-7] info c.a.a.rvice.impl.asyncrviceimpl – end executeasync
10:32:30.001 [async-rvice-8] info c.a.a.rvice.impl.asyncrviceimpl – start executeasync
异步线程执行批量插入等耗时任务
10:32:30.001 [async-rvice-8] info c.a.a.rvice.impl.asyncrviceimpl – end exec日落即景作文400字uteasync
10:32:31.001 [async-rvice-1] info c.a.a.rvice.impl.asyncrviceimpl – start executeasync
异步线程执行批量插入等耗时任务
10:32:31.001 [async-rvice-1] info c.a.a.rvice.impl.asyncrviceimpl – end executeasync
还没完:
通过以上日志可以发现,[async-rvice-]是有多个线程的,显然已经在我们配置的线程池中执行了,表明每次请求都快速响应了,而耗时的操作都留给线程池中的线程去异步执行;
还有另一个问提就是,虽然已经用上了线程池,但是依然不清楚线程池当时的情况,有多少线程在执行,多少在队列中等待呢?于是这里我创建了一个threadpooltaskexecutor的子类,可以把每次提交线程的时候都会将当前线程池的运行状况打印出来
import lombok.extern.slf4j.slf4j; @slf4jpublic class visiablethreadpooltaskexecutor extends threadpooltaskexecutor { /** * */ private static final long rialversionuid = -3518460523928455463l; private void showthreadpoolinfo(string prefix) { threadpoolexecutor threadpoolexecutor = getthreadpoolexecutor(); if (null == threadpoolexecutor) { return; } log.info("{}, {},taskcount [{}], completedtaskcount [{}], activecount [{}], queuesize [{}]", this.getthreadnameprefix(), 山西长治学院 prefix, threadpoolexecutor.gettaskcount(), threadpoolexecutor.getcompletedtaskcount(), threadpoolexecutor.getactivecount(), threadpoolexecutor.getqueue().size()); } @override public void execute(runnable task) { showthreadpoolinfo("1. do execute"); super.execute(task); } @override public void execute(runnable task, long starttimeout) { showthreadpoolinfo("2. do execute"); super.execute(task, starttimeout); } @override public future<?> submit(runnable task) { showthreadpoolinfo("1. do submit"); return super.submit(task); } @override public <t> future<t> submit(callable<t> task) { showthreadpoolinfo("2. do submit"); 女王的教师 return super.submit(task); } @override public listenablefuture<?> submitlistenable(runnable task) { showthreadpoolinfo("1. do submitlistenable"); return super.submitlistenable(task); } @override public <t> listenablefuture<t> submitlistenable(callable<t> task) { showthreadpoolinfo("2. do submitlistenable"); return super.submitlistenable(task); } }
其次:修改rcexecutorconfig.java的asyncrviceexecutor方法,
将threadpooltaskexecutor executor = new threadpooltaskexecutor()改为threadpooltaskexecutor executor = new visiablethreadpooltaskexecutor()
@bean(name = "asyncrviceexecutor") public executor asyncrviceexecutor() { log.info("start asyncrvicee一个人寂寞xecutor"); //threadpooltaskexecutor executor = new threadpooltaskexecutor(); threadpooltaskexecutor executor = new visiablethreadpooltaskexecutor(); //配置核心线程数 executor.tcorepoolsize(corepoolsize); //配置最大线程数 executor.tmaxpoolsize(maxpoolsize); //配置队列大小 executor.tqueuecapacity(queuecapacity); //配置线程池中的线程的名称前缀 executor.tthreadnameprefix(nameprefix); // rejection-policy:当pool已经达到max size的时候,如何处理新任务 // caller_runs:不在新线程中执行任务,而是有调用者所在的线程来执行 executor.trejectedexecutionhandler(new threadpoolexecutor.callerrunspolicy()); //执行初始化 executor.initialize(); return executor; }
测试结果如下:
异步线程执行批量插入等耗时任务
10:41:35.003 [async-rvice-5] info c.a.a.rvice.impl.asyncrviceimpl – end executeasync
10:41:36.001 [sheduled-pool-1-thread-1] info c.a.a.e.t.visiablethreadpooltaskexecutor – async-rvice-, 2. do submit,taskcount [5], completedtaskcount [5], activecount [0], queuesize [0]
10:41:36.001 [async-rvice-6] info c.a.a.rvice.impl.asyncrviceimpl – start executeasync
异步线程执行批量插入等耗时任务
10:41:36.002 [async-rvice-6] info c.a.a.rvice.impl.asyncrviceimpl – end executeasync
10:41:37.001 [sheduled-pool-1-thread-7] info c.a.a.e.t.visiablethreadpooltaskexecutor – async-rvice-, 2. do submit,taskcount [6], completedtaskcount [6], activecount [0], queuesize [0]
10:41:37.001 [async-rvice-7] info c.a.a.rvice.impl.asyncrviceimpl – start executeasync
异步线程执行批量插入等耗时任务
10:41:37.002 [async-rvice-7] info c.a.a.rvice.impl.asyncrviceimpl – end executeasync
10:41:38.002 [sheduled-pool-1-thread-7] info c.a.a.e.t.visiablethreadpooltaskexecutor – async-rvice-, 2. do submit,taskcount [7], completedtaskcount [7], activecount [0], queuesize [0]
10:41:38.002 [async-rvice-8] info c.a.a.rvice.impl.asyncrviceimpl – start executeasync
异步线程执行批量插入等耗时任务
10:41:38.002 [async-rvice-8] info c.a.a.rvice.impl.asyncrviceimpl – end executeasync
10:41:39.001 [sheduled-pool-1-thread-7] info c.a.a.e.t.visiablethreadpooltaskexecutor – async-rvice-, 2. do submit,taskcount [8], completedtaskcount [8], activecount [0], queuesize [0]
10:41:39.001 [async-rvice-1] info c.a.a.rvice.impl.asyncrviceimpl – start executeasync
异步线程执行批量插入等耗时任务
10:41:39.002 [async-rvice-1] info c.a.a.rvice.impl.asyncrviceimpl – end executeasync
10:41:40.003 [sheduled-pool-1-thread-7] info c.a.a.e.t.visiablethreadpooltaskexecutor – async-rvice-, 2. do submit,taskcount [9], completedtaskcount [9], activecount [0], queuesize [0]
10:41:40.003 [async-rvice-2] info c.a.a.rvice.impl.asyncrviceimpl – start executeasync
异步线程执行批量插入等耗时任务
10:41:40.003 [async-rvice-2] info c.a.a.rvice.impl.asyncrviceimpl – end executeasync
10:41:41.001 [sheduled-pool-1-thread-7] info c.a.a.e.t.visiablethreadpooltaskexecutor – async-rvice-, 2. do submit,taskcount [10], completedtaskcount [10], activecount [0], queuesize [0]
10:41:41.001 [async-rvice-3] info c.a.a.rvice.impl.asyncrviceimpl – start executeasync
异步线程执行批量插入等耗时任务
10:41:41.001 [async-rvice-3] info c.a.a.rvice.impl.asyncrviceimpl – end executeasync
10:41:42.000 [sheduled-pool-1-thread-7] info c.a.a.e.t.visiablethreadpooltaskexecutor – async-rvice-, 2. do submit,taskcount [11], completedtaskcount [11], activecount [0], queuesize [0]
10:41:42.000 [async-rvice-4] info c.a.a.rvice.impl.asyncrviceimpl – start executeasync
异步线程执行批量插入等耗时任务
10:41:42.000 [async-rvice-4] info c.a.a.rvice.impl.asyncrviceimpl – end executeasync
10:41:43.001 [sheduled-pool-1-thread-7] info c.a.a.e.t.visiablethreadpooltaskexecutor – async-rvice-, 2. do submit,taskcount [12], completedtaskcount [12], activecount [0], queuesize [0]
10:41:43.002 [async-rvice-5] info c.a.a.rvice.impl.asyncrviceimpl – start executeasync
异步线程执行批量插入等耗时任务
10:41:43.003 [async-rvice-5] info c.a.a.rvice.impl.asyncrviceimpl – end executeasync
10:41:44.001 [sheduled-pool-1-thread-7] info c.a.a.e.t.visiablethreadpooltaskexecutor – async-rvice-, 2. do submit,taskcount [13], completedtaskcount [13], activecount [0], queuesize [0]
10:41:44.001 [async-rvice-6] info c.a.a.rvice.impl.asyncrviceimpl – start executeasync
异步线程执行批量插入等耗时任务
10:41:44.001 [async-rvice-6] info c.a.a.rvice.impl.asyncrviceimpl – end executeasync
10:41:45.000 [sheduled-pool-1-thread-7] info c.a.a.e.t.visiablethreadpooltaskexecutor – async-rvice-, 2. do submit,taskcount [14], completedtaskcount [14], activecount [0], queuesize [0]
10:41:45.001 [async-rvice-7] info c.a.a.rvice.impl.asyncrviceimpl – start executeasync
解释:这里意思提交了14个任务,处理了14个任务,对列中还剩0个任务
10:41:45.000 [sheduled-pool-1-thread-7] info c.a.a.e.t.visiablethreadpooltaskexecutor – async-rvice-, 2. do submit,taskcount [14], completedtaskcount [14], activecount [0], queuesize [0]
到此为止就ok了!
以上为个人经验,希望能给大家一个参考,也希望大家多多支持www.887551.com。
本文发布于:2023-04-04 08:08:03,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/a58384a4f9e8eb109ae32c491e470ff8.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:Springboot线程池并发处理数据优化方式.doc
本文 PDF 下载地址:Springboot线程池并发处理数据优化方式.pdf
留言与评论(共有 0 条评论) |