SpringBoot实现多线程定时任务,超简单

本文详细介绍了如何在SpringBoot项目中利用@EnableScheduling和@Scheduled注解创建并执行五个定时任务,通过多线程确保任务独立并发。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

1.创建SpringBoot项目

1.1项目结构

1.2导入基本依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

2.两个关键注解

@EnableScheduling:标注在主启动类上,开启定时任务

@Scheduled:标注在方法上,定时任务入口,后面可以自定义定时任务的频率。例如:@Scheduled(cron = "*/1 * * * * ?")

3.代码

3.1主启动类

package com.wangjw;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class ScheduleApplication {

    public static void main(String[] args) {
        SpringApplication.run(ScheduleApplication.class, args);
    }

}

3.2service层 创建5个定时任务

package com.wangjw.service;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

/**
 * @author wangjw
 * @date 2021 年 05 月 28 日
 */
@Service
public class ScheduleTask {

    @Scheduled(cron = "*/1 * * * * ?")
    public void task1(){
        System.out.println(Thread.currentThread().getName()+"调度任务1正在执行");
        try {
            Thread.sleep(3000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Scheduled(cron = "*/1 * * * * ?")
    public void task2(){
        System.out.println(Thread.currentThread().getName()+"调度任务2正在执行");
        try {
            Thread.sleep(3000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    @Scheduled(cron = "*/1 * * * * ?")
    public void task3(){
        System.out.println(Thread.currentThread().getName()+"调度任务3正在执行");
        try {
            Thread.sleep(3000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    @Scheduled(cron = "*/1 * * * * ?")
    public void task4(){
        System.out.println(Thread.currentThread().getName()+"调度任务4正在执行");
        try {
            Thread.sleep(3000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    @Scheduled(cron = "*/1 * * * * ?")
    public void task5(){
        System.out.println(Thread.currentThread().getName()+"调度任务5正在执行");
        try {
            Thread.sleep(3000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

3.3开启多线程执行

多个任务时,建议使用多线程,保证多个任务独立运行,互不影响

package com.wangjw.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import java.util.concurrent.Executors;

/**
 * @author wangjw
 * @date 2021 年 05 月 28 日
 */
@Configuration
public class ScheduleConfig implements SchedulingConfigurer {
    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        // 配置线程池的大小:需要根据实际的电脑配置来调整
        scheduledTaskRegistrar.setScheduler(Executors.newScheduledThreadPool(5));
    }
}

4.运行结果

pool-1-thread-2调度任务2正在执行
pool-1-thread-3调度任务3正在执行
pool-1-thread-1调度任务1正在执行
pool-1-thread-4调度任务5正在执行
pool-1-thread-5调度任务4正在执行
pool-1-thread-4调度任务4正在执行
pool-1-thread-5调度任务5正在执行
pool-1-thread-1调度任务3正在执行
pool-1-thread-3调度任务1正在执行
pool-1-thread-2调度任务2正在执行
pool-1-thread-3调度任务1正在执行

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值