使用ScheduledExecutorService线程池创建定时任务

简介: 使用ScheduledExecutorService线程池创建定时任务
@RestController
@RequestMapping("/schedule")
public class ScheduledController {
    public static Map<String,Boolean> taskInfo = new HashMap<>(4);
    @PostConstruct
    public void init(){
        taskInfo.put("at",true);
        taskInfo.put("with",true);
    }
    @GetMapping("/stop/task/{taskName}")
    public String stopTask(@PathVariable String taskName){
        taskInfo.put(taskName,false);
        return "ok";
    }
    @GetMapping("/execute/at/{initialDelay}")
    public String executeAt(@PathVariable Integer initialDelay){
        taskInfo.put("at",true);
        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
        //以上一个任务开始的时间计时,period时间过去后,检测上一个任务是否执行完毕,如果上一个任务执行完毕,则当前任务立即执行,如果上一个任务没有执行完毕,则需要等上一个任务执行完毕后立即执行。
        executorService.scheduleAtFixedRate(()->{
            if(!taskInfo.get("at")){
                throw new RuntimeException("任务停止-----------");
            }
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + " " + TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()));
        },initialDelay,2, TimeUnit.SECONDS);
        return "ok";
    }
    @GetMapping("/execute/with/{initialDelay}")
    public String executeWith(@PathVariable Integer initialDelay){
        taskInfo.put("with",true);
        ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
        //以上一个任务结束时开始计时,period时间过去后,立即执行
        executorService.scheduleWithFixedDelay(()->{
            if(!taskInfo.get("with")){
                throw new RuntimeException("任务停止-----------");
            }
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()));
        },initialDelay,2, TimeUnit.SECONDS);
        return "ok";
    }
}
目录
打赏
0
相关文章
JUC线程池: ScheduledThreadPoolExecutor详解
`ScheduledThreadPoolExecutor`是Java标准库提供的一个强大的定时任务调度工具,它让并发编程中的任务调度变得简单而可靠。这个类的设计兼顾了灵活性与功能性,使其成为实现复杂定时任务逻辑的理想选择。不过,使用时仍需留意任务的执行时间以及系统的实际响应能力,以避免潜在的调度问题影响应用程序的行为。
115 1
使用scheduleAtFixedRate进行定时任务调度
使用scheduleAtFixedRate进行定时任务调度
ScheduledExecutorService与ExecutorService区别
ScheduledExecutorService与ExecutorService区别
使用ScheduledThreadPoolExecutor进行任务调度
使用ScheduledThreadPoolExecutor进行任务调度
使用ScheduledExecutorService进行任务调度
使用ScheduledExecutorService进行任务调度
ScheduledExecutorService:多线程任务调度
ScheduledExecutorService:多线程任务调度
803 0
ScheduledExecutorService:多线程任务调度
调度线程池ScheduledThreadPoolExecutor的正确使用姿势
调度线程池ScheduledThreadPoolExecutor的正确使用姿势
5663 1
调度线程池ScheduledThreadPoolExecutor的正确使用姿势
调度线程池ScheduledThreadPoolExecutor源码解析
调度线程池ScheduledThreadPoolExecutor源码解析
171 0
调度线程池ScheduledThreadPoolExecutor源码解析
【多线程】线程池 | ScheduledThreadPoolExecutor
ScheduledThreadPoolExecutor继承ThreadPoolExecutor对execute和submit进行了重写, 同时也实现了ScheduledExecutorService特有的方法。
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等