线程池应用_定时执行任务

82 阅读1分钟

直接上代码,解决 "如何让每周四 18:00:00 定时执行任务 ?"类似问题

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.concurrent.*;

/**
 * @author : 其然乐衣Letitbe
 * @date : 2022/10/15
 */
public class TestSchedule {

    /**
     *如何让每周四 18:00:00 定时执行任务 ?
     */
    public static void main(String[] args) {
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        System.out.println( now );

        // 获取周四时间
        LocalDateTime time = now.withHour(18).withMinute(0).withSecond(0).withNano(0).with(DayOfWeek.THURSDAY);
        // 如果 当前时间 > 本周四时间,必须找到下周周四
        if ( now.compareTo( time ) >0 ) {
            // 加一周
            time = time.plusWeeks( 1 );
        }
        System.out.println( time );

        // initialDelay 代表当前时间和周四的时间差
        // period 一周的间隔时间
        long initialDelay = Duration.between(now, time).toMillis();
        long period = 1000 * 60 * 60 * 24 * 7;
        ScheduledExecutorService pool = Executors.newScheduledThreadPool( 1 );
        pool.scheduleAtFixedRate( () -> {
            System.out.println( "running..." );
        }, initialDelay, period, TimeUnit.MILLISECONDS );

    }

}

Duration类专门用于做时间间隔运算的,如Duration.between( 时间1,时间2 ).toMillis()