java 以及 springboot 分别实现定时器

一、曾经的三种方法

package com.example.demo;

import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class AllController {

    public static void main(String[] args) {
//        dd();
//        ee();
//        ff();

    }


    /**
     * Runnable
     */
    public static void ff() {
        Runnable runnable = new Runnable() {
            public void run() {
                //要执行的方法
                System.out.println("HelloWorld!!!" + LocalDateTime.now());
            }
        };
        // ScheduledExecutorService:是从Java SE5的java.util.concurrent里,做为并发工具类被引进的,这是最理想的定时任务实现方式。
        ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
        // 第一个参数为第一次执行的时间,第二个参数为首次执行的延时时间,第三个参数为定时执行的间隔时间
        service.scheduleAtFixedRate(runnable, 1, 5, TimeUnit.SECONDS);
    }

    /**
     * Timer是一个定时器工具,用来执行指定任务
     * TimerTask是一个抽象类,他的子类可以代表一个被Timer计划的任务
     */
    public static void ee() {
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                //要执行的方法
                System.out.println("HelloWorld!!!" + LocalDateTime.now());
            }
        };
        Timer timer = new Timer();
        // 定义开始等待时间  --- 等待 5 秒
        long wait = 5000;
        // 定义每次执行的间隔时间
        long wait1 = 1 * 1000;
        // 安排任务在一段时间内运行
        timer.scheduleAtFixedRate(task, wait, wait1);
    }

    /**
     * thread
     */
    public static void dd() {
        // 每一秒钟执行一次
        final long timeInterval = 1000;
        Runnable runnable = new Runnable() {
            public void run() {
                while (true) {
                    //要执行的方法
                    System.out.println("HelloWorld!!!" + LocalDateTime.now());
                    try {
                        //同步延迟数据,并且会阻塞线程
                        Thread.sleep(timeInterval);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        //创建定时器
        Thread thread = new Thread(runnable);
        //开始执行
        thread.start();
    }
}

二、springboot 下

第一种

1、在启动类上添加注解@EnableScheduling开启定时器

@SpringBootApplication
@EnableScheduling
public class DemoApplication {

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

}

2、给要定时执行的方法上添加注解@Scheduled(cron = "0 0 0 * * * ")

package com.example.demo;

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

import java.time.LocalDateTime;

@Component
public class ScheduleTask1 {

    @Scheduled(cron = "0/2 * * * * *")
    public void runTimer() {
        System.out.println("HelloWorld!!!" + LocalDateTime.now());
    }
}

第二种

直接在定时器类上添加@Configuration、@EnableScheduling注解,标注这个类是配置文件,并开启定时开关;然后给要定时执行方法上添加@Scheduled(cron = "0 0 0 * * * ")

package com.example.demo;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDateTime;

@Configuration      //1.主要用于标记配置类,兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class ScheduleTask {

    //3.添加定时任务
    @Scheduled(cron = "0/5 * * * * ?")
    //或直接指定时间间隔,例如:5秒 @Scheduled(fixedRate=5000)
    public void tasks() {
        System.out.println("HelloWorld!!!" + LocalDateTime.now());
    }
}

cron表达式

格式: cron = [秒] [分] [小时] [日] [月] [周] [年]

注:最后一位 年 可不填

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值