@Scheduled默认创建的线程是单线程,任务的执行会受到上一个任务的影响,创建定时任务也比较简单

1
2
3
4
5
6
7
8
9
10
11
12
@Component
@Configuration //1.主要用于标记配置类,兼备Component的效果。
@EnableScheduling // 2.开启定时任务
public class ScheduledTask {
//3.添加定时任务
@Scheduled(cron = "0/5 * * * * ?")
//或直接指定时间间隔,例如:5秒
//@Scheduled(fixedRate=5000)
private void configureTasks() {
System.err.println("执行静态定时任务时间: " + LocalDateTime.now());
}
}

Cron的表达式为 秒(0-59) 分(0~59)时(0~23)日(0~31)的某天,需计算月(0~11)周几( 可填1-7 或 SUN/MON/TUE/WED/THU/FRI/SAT)
“0/5 ?”可以解析成 每5秒执行一次,其他不指定,此时开启application,控制台没隔5秒打印一次

基于接口的定时任务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Component
@Configuration //1.主要用于标记配置类,兼备Component的效果。
@EnableScheduling // 2.开启定时任务
public class DynamicScheduleTask implements SchedulingConfigurer { @Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.addTriggerTask(
//1.添加任务内容(Runnable),匿名
() -> System.out.println("执行动态定时任务: " + LocalDateTime.now().toLocalTime()),
//2.设置执行周期(Trigger)
triggerContext -> {
return new CronTrigger("0/5 * * * * ?").nextExecutionTime(triggerContext);
}
);
}
}

多线程的定时任务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@Component
@EnableScheduling // 1.开启定时任务
@EnableAsync // 2.开启多线程
public class MultiThreadScheduleTask { @Async
@Scheduled(fixedDelay = 1000) //间隔1秒
public void first() throws InterruptedException {
System.out.println("第一个定时任务开始 : " + LocalDateTime.now().toLocalTime() + "rn线程 : " + Thread.currentThread().getName());
大专栏  spring boot 创建定时任务s="line"> System.out.println();
Thread.sleep(1000 * 10);
} @Async
@Scheduled(fixedDelay = 2000)
public void second() {
System.out.println("第二个定时任务开始 : " + LocalDateTime.now().toLocalTime() + "rn线程 : " + Thread.currentThread().getName());
System.out.println();
}
}

这个定时任务随着时间的增加不断的增加线程,这肯定会消耗大量的资源,因此在配置多线程的定时任务时,常常需要设置一个线程池来避免资源消耗过多

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@EnableAsync
@Configuration
public class TaskPoolConfig {
@Bean("taskExecutor")
public Executor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10);
executor.setMaxPoolSize(20);
executor.setQueueCapacity(200);
executor.setKeepAliveSeconds(60);
executor.setThreadNamePrefix("taskExecutor-");
executor.setWaitForTasksToCompleteOnShutdown(true);//设置线程在任务执行完之后才x释放
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
return executor;
}
}

使用Future来获取Runable的执行结果

1
2
3
4
5
6
7
8
9
10
11
12
13
@Slf4j
@Component
public class Task {
public static Random random = new Random();
@Async("taskExecutor")
public Future<String> run() throws Exception {
long sleep = random.nextInt(10000);
log.info("开始任务,需耗时:" + sleep + "毫秒");
Thread.sleep(sleep);
log.info("完成任务");
return new AsyncResult<>("test");
}
}

定义超时时间并释放线程

1
2
3
4
5
6
7
8
9
10
11
12
13
@Slf4j
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class ApplicationTests {
@Autowired
private Task task;
@Test
public void test() throws Exception {
Future<String> futureResult = task.run();
String result = futureResult.get(5, TimeUnit.SECONDS);
log.info(result);
}
}

最新文章

  1. Entity Framework 6 Recipes 2nd Edition(9-4)译-&gt;Web API 的客户端实现修改跟踪
  2. 【CodeVS2800】 送外卖 最短路+状压DP
  3. 组合or继承
  4. 微信小程序里碰到的坑和小知识
  5. java验证码组件kaptcha使用方法
  6. Auto Updating the exe from a network location when application starts z
  7. Python基本语法[二],python入门到精通[四] (转)
  8. LWP::UserAgent介绍3 -&gt; cookie设置
  9. HNU 13064 Cuckoo for Hashing解题报告 North America - East Central 2013
  10. Mecanim动画系统
  11. 小白的Python之路 day5 shelve模块讲解
  12. .NET ClrProfiler ILRewrite 商业级APM原理
  13. 细说addEventListener与事件捕获
  14. hashMap源码分析1--翻译
  15. CF1096D Easy Problem
  16. 微探eventlet.monkey_patch
  17. java离线地图web GIS制作
  18. MySQL5.7修改默认密码、随机密码
  19. linux操作系统-源码包安装jdk1.7
  20. 移植Python3到TQ2440(二)

热门文章

  1. Properties in Algebra
  2. vscode template中设置不换行
  3. 用IDLE调试python程序
  4. tensorflow实现卷积层的几种方式
  5. POJ 1O17 Packets [贪心]
  6. python 知识点补充
  7. jenkins-master-slave节点配置总结
  8. ZZJ_淘淘商城项目:day02(淘淘商城01 - 项目讲解、环境搭建)
  9. SQL服务器攻击总结-注入
  10. 新年在家学java之基础篇-参数&amp;修饰符&amp;构造器