From b726dc97538b7d4ba71fd06c14161017dd67c2b7 Mon Sep 17 00:00:00 2001 From: xubinbin <1323875150@qq.com> Date: Wed, 10 May 2023 15:38:22 +0800 Subject: [PATCH] =?UTF-8?q?"@schedule"=E6=98=AFSpring=E6=A1=86=E6=9E=B6?= =?UTF-8?q?=E6=8F=90=E4=BE=9B=E7=9A=84=E4=B8=80=E7=A7=8D=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E6=89=A7=E8=A1=8C=E6=9C=BA=E5=88=B6=EF=BC=8C?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E6=83=85=E5=86=B5=E4=B8=8B=E5=AE=83=E6=98=AF?= =?UTF-8?q?=E5=8D=95=E7=BA=BF=E7=A8=8B=E6=89=A7=E8=A1=8C=EF=BC=8C=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE=E4=B8=AD=E5=A4=9A=E6=AC=A1=E4=BD=BF=E7=94=A8fixedRate?= =?UTF-8?q?=E6=8C=89=E6=8C=87=E5=AE=9A=E9=A2=91=E7=8E=87=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1(=E4=B8=8D=E7=AE=A1=E5=89=8D=E9=9D=A2?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E6=98=AF=E5=90=A6=E5=B7=B2=E7=BB=8F=E5=AE=8C?= =?UTF-8?q?=E6=88=90)=EF=BC=8C=E5=9C=A8=E5=90=8C=E6=97=B6=E6=89=A7?= =?UTF-8?q?=E8=A1=8C=E5=A4=9A=E4=B8=AA=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E6=97=B6=E5=8F=AF=E8=83=BD=E4=BC=9A=E5=87=BA=E7=8E=B0=E9=98=BB?= =?UTF-8?q?=E5=A1=9E=E5=92=8C=E6=80=A7=E8=83=BD=E9=97=AE=E9=A2=98=EF=BC=8C?= =?UTF-8?q?=E4=B8=BA=E4=BA=86=E8=A7=A3=E5=86=B3=E8=BF=99=E7=A7=8D=E5=8D=95?= =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E7=93=B6=E9=A2=88=E9=97=AE=E9=A2=98=EF=BC=8C?= =?UTF-8?q?=E5=B0=86=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1=E7=9A=84=E6=89=A7?= =?UTF-8?q?=E8=A1=8C=E6=9C=BA=E5=88=B6=E6=94=B9=E4=B8=BA=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=A4=9A=E7=BA=BF=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../iot/vmp/conf/ScheduleConfig.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/main/java/com/genersoft/iot/vmp/conf/ScheduleConfig.java diff --git a/src/main/java/com/genersoft/iot/vmp/conf/ScheduleConfig.java b/src/main/java/com/genersoft/iot/vmp/conf/ScheduleConfig.java new file mode 100644 index 00000000..432fafbb --- /dev/null +++ b/src/main/java/com/genersoft/iot/vmp/conf/ScheduleConfig.java @@ -0,0 +1,30 @@ +package com.genersoft.iot.vmp.conf; + +import org.apache.commons.lang3.concurrent.BasicThreadFactory; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.SchedulingConfigurer; +import org.springframework.scheduling.config.ScheduledTaskRegistrar; + +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.ThreadPoolExecutor; + +/** + * "@Scheduled"是Spring框架提供的一种定时任务执行机制,默认情况下它是单线程的,在同时执行多个定时任务时可能会出现阻塞和性能问题。 + * 为了解决这种单线程瓶颈问题,可以将定时任务的执行机制改为支持多线程 + */ +@Configuration +public class ScheduleConfig implements SchedulingConfigurer { + + public static final int cpuNum = Runtime.getRuntime().availableProcessors(); + + private static final int corePoolSize = cpuNum; + + private static final String threadNamePrefix = "scheduled-task-pool-%d"; + + @Override + public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { + taskRegistrar.setScheduler(new ScheduledThreadPoolExecutor(corePoolSize, + new BasicThreadFactory.Builder().namingPattern(threadNamePrefix).daemon(true).build(), + new ThreadPoolExecutor.CallerRunsPolicy())); + } +}