外贸网站建设 翻译,wordpress模板怎么安装教程视频,中国建设银行内部网站,动效网站怎么做我们在Java Scheduled定时任务中已经学到了如何开启定时任务#xff0c;但却在同时开启多个定时任务的时候#xff0c;遇到了新的问题#xff0c;Scheduled定时任务是单线程的#xff0c;无法满足同一个时间开启多个定时任务#xff0c;因此#xff0c;我们进行了如下调整…我们在Java Scheduled定时任务中已经学到了如何开启定时任务但却在同时开启多个定时任务的时候遇到了新的问题Scheduled定时任务是单线程的无法满足同一个时间开启多个定时任务因此我们进行了如下调整
1、新建一个配置类
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;import java.util.concurrent.Executor;Configuration
EnableAsyncpublic class AsyncConfig {private int corePoolSize 10;private int maxPoolSize 200;private int queueCapacity 10;Beanpublic Executor taskExecutor() {ThreadPoolTaskExecutor executor new ThreadPoolTaskExecutor();executor.setCorePoolSize(corePoolSize);executor.setMaxPoolSize(maxPoolSize);executor.setQueueCapacity(queueCapacity);executor.initialize();return executor;}
}2、在定时任务中添加Async异步注解即可开启多线程定时任务。
但通过如上方法又会出现新的问题如果某个定时任务在规格的时间内没有运行完而它新的定时任务再次开启就会出现两个定时任务同时对表的数据进行操作会导致数据各种的错误因此再次对定时任务进行处理增加锁机制synchronized对未执行完的定时任务及时锁住不让它开启下一个定时任务。
实例如下所示 private static Object lkm1 new Object();private static Object lkm2 new Object();/**** description:* author: lkm* date: 2023/9/7 9:15**/Scheduled(cron 0 0/1 * * * ?)public void scheduledTask1() throws Exception {synchronized(lkm1){Date date new Date();System.out.println(任务开始1);System.out.println(new SimpleDateFormat(yyyy-MM-dd HH:mm:ss).format(date));Thread.sleep(120000);//2分钟System.out.println(任务1,结束);}}/**** description:* author: lkm* date: 2023/9/7 9:15**/Scheduled(cron 0 0/1 * * * ?)public void scheduledTask2() throws Exception {synchronized(lkm2){Date date new Date();System.out.println(任务开始2);System.out.println(new SimpleDateFormat(yyyy-MM-dd HH:mm:ss).format(date));Thread.sleep(120000);//2分钟System.out.println(任务2,结束);}}