SpringBoot多线程与任务调度总结

一、前言

多线程与任务调度是java开发中必须掌握的技能,在springBoot的开发中,多线程和任务调度变得越来越简单。实现方式可以通过实现ApplicationRunner接口,重新run的方法实现多线程。任务调度则可以使用@Scheduled注解

二、使用示例

java 复制代码
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class ToolServiceThread implements ApplicationRunner {

	@Autowired
	private TmpMUserService tmpMUserService;

	@Override
	public void run(ApplicationArguments args) throws Exception {
		log.info("开启线程.....,");

		// 处理用户手机号
		// tmpMUserService.updateOne();

	}

	/**
	 * 定时任务 早上0点10分
	 */
	// @Scheduled(cron = "0 10 0 * * ?")
   @Scheduled(cron = "0 0/10 * * * ?") // 每10分钟刷新
	public void hotelTask() throws Exception {

	}

}

三、java定时任务多种实现方式

  1. 使用java.util.Timer类
java 复制代码
import java.util.Timer;  
import java.util.TimerTask;  
  
public class TimerExample {  
    public static void main(String[] args) {  
        Timer timer = new Timer();  
        TimerTask task = new TimerTask() {  
            @Override  
            public void run() {  
                System.out.println("执行定时任务!");  
            }  
        };  
        // 延迟5秒后开始执行任务,然后每隔2秒执行一次  
        timer.schedule(task, 5000, 2000);  
    }  
}

2.使用ScheduledExecutorService

ScheduledExecutorService 是Java 5及以上版本中提供的一个更加强大和灵活的定时任务执行器。

java 复制代码
import java.util.concurrent.Executors;  
import java.util.concurrent.ScheduledExecutorService;  
import java.util.concurrent.TimeUnit;  
  
public class ScheduledExecutorExample {  
    public static void main(String[] args) {  
        ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();  
        executor.scheduleAtFixedRate(() -> {  
            System.out.println("执行定时任务!");  
        }, 5, 2, TimeUnit.SECONDS); // 延迟5秒后开始执行,然后每隔2秒执行一次  
    }  
}

3.使用Spring的@Scheduled注解 (适用于Spring Boot应用)

如果你正在使用Spring Boot,你可以使用@Scheduled注解来轻松地实现定时任务。首先,确保你的Spring Boot应用已经启用了定时任务支持:在主类上添加@EnableScheduling注解。然后,你可以在方法上添加@Scheduled注解来指定任务的执行计划。

java 复制代码
import org.springframework.scheduling.annotation.EnableScheduling;  
import org.springframework.scheduling.annotation.Scheduled;  
import org.springframework.stereotype.Component;  
  
@Component  
@EnableScheduling  
public class ScheduledTasks {  
    @Scheduled(fixedRate = 5000) // 每隔5秒执行一次  
    public void doSomething() {  
        System.out.println("执行定时任务!");  
    }  
}

四、java定时任务应用场景

  1. 定时发送通知:在企业管理系统中,可以设置定时任务,定期向员工发送通知,例如会议提醒、任务更新等。
  2. 定时执行报表生成:在财务或销售部门,可以设置定时任务,定期生成报表,例如每日销售报表、月度财务报表等。
  3. 定时处理订单:在电商或物流系统中,可以设置定时任务,自动处理订单状态,例如发货、确认收货等。
  4. 定时清理缓存:对于使用缓存的系统,定时清理过期或无效的缓存可以提高系统的性能和稳定性。
  5. 定时备份数据库:类似于其他语言中的例子,Java 程序也可以设置定时任务,定期备份数据库数据。
  6. 定时启动或停止服务:在服务器管理中,可以设置定时任务,自动启动或停止服务,例如在夜间关闭一些非必要的服务以节省资源。
  7. 定时发送邮件或短信:Java 程序可以与邮件或短信服务集成,定时发送邮件或短信通知,例如会议提醒、生日祝福等。
  8. 定时执行批处理任务:对于需要批量处理数据的场景,可以设置定时任务,自动执行批处理任务,例如数据导入、数据导出等。
相关推荐
江华森9 分钟前
Python神经网络编程(四):Python从零搭建神经网络
开发语言·python·神经网络
zhanghaofaowhrql15 分钟前
Spring Data JPA Repository 详解:从基础到高级用法
java·数据库·sql
编程(变成)小辣鸡1 小时前
如何防止接口被恶意刷?
java·后端·网络安全
Helen_cai1 小时前
OpenHarmony 完整项目工程整合规范 + 模块化分层架构(API23+ 标准企业级结构)
开发语言·华为·php·harmonyos
大圣编程6 小时前
Java 多维数组详解
java·开发语言
万法若空9 小时前
【算法-查找】查找算法
java·数据结构·算法
殳翰9 小时前
下服务器端开发流程及相关工具介绍(C++)
开发语言·c++
落寞的星星9 小时前
这个对象就包含了已经转换好的DFA和各种词法分析器运转所需要的参数。下一步,我们就可以用ScannerInfo对象创建出Scanner对象,请看下面的代码:
开发语言·c#
nothing&nowhere10 小时前
用 Python 做问卷数据清洗:无效样本检测与处理实战
开发语言·python·数据清洗·数据处理·问卷星·问卷星脚本·刷问卷
2601_9615934210 小时前
Rust 开发环境配置繁琐?RustRover 开箱即用搞定编码调试
开发语言·后端·macos·rust