美文网首页
JAVA定时任务

JAVA定时任务

作者: 松松木tell | 来源:发表于2022-05-23 15:30 被阅读0次

java 中采用ScheduledExecutorService来进行定时任务调度。
ScheduledExecutorService是基于线程池设计的定时任务类,每个调度任务都会分配到线程池中的一个线程去执行,也就是说,任务是并发执行,互不影响。

一、定时任务简单应用

1、简单的列子

public class Schedule1 {
    public static void main(String[] args) {
        ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(10);
        Runnable runnable = () -> {
            String name = Thread.currentThread().getName();
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
            System.out.println(name + ":RUN1 正在执行任务!" + format.format(new Date()));
        };
        scheduledThreadPool.scheduleAtFixedRate(runnable, 0, 2, TimeUnit.SECONDS);
    }
}

2、ScheduledExecutorService

定时任务执行接口,有4中执行方法。

  • schedule(Runnable command,long delay, TimeUnit unit);
    创建并执行在给定延迟后启用的ScheduledFuture。
  • schedule(Callable<V> callable, long delay, TimeUnit unit);
    创建并执行在给定延迟后启用的单次操作。
  • scheduleAtFixedRate(Runnable command,long initialDelay,long period,TimeUnit unit); 固定频率
    首次是initialDelay+period;下次就间隔period执行;计时是按照上个开始时间计算,如果任务执行时间超过period,那下次就直接执行了。如果超过多个period,下次多个都会同时执行。
    重点:一次执行失败会导致,整个调度失败,务必扑捉住错误。
  • scheduleWithFixedDelay(Runnable command,long initialDelay,long delay,TimeUnit unit); 固定延迟
    在上一个结束后和下一个开始之间给定延迟。

ScheduledThreadPoolExecutor

作为ScheduledExecutorService的具体实现类。创建实例时候我们可以直接new,但我们一般采用Executors 去创建实例。

 ScheduledExecutorService scheduledThreadPool1=new ScheduledThreadPoolExecutor(10);

Executors

为方便管理线程,采用Executors 工厂方法去创建实例。

public class Executors {
    public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
        return new ScheduledThreadPoolExecutor(corePoolSize);
    }
   public static ScheduledExecutorService newScheduledThreadPool(
            int corePoolSize, ThreadFactory threadFactory) {
        return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);
    }
    public static ScheduledExecutorService newScheduledThreadPool(
            int corePoolSize, ThreadFactory threadFactory) {
        return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);
    }
   public static ScheduledExecutorService newSingleThreadScheduledExecutor() {
        return new DelegatedScheduledExecutorService
            (new ScheduledThreadPoolExecutor(1));
    }
}

取消定时任务

import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        scheduler.setPoolSize(5);
        scheduler.setRemoveOnCancelPolicy(true);//cancel时会删除这个任务
        scheduler.initialize();
        ScheduledFuture<?> future=scheduler.scheduleAtFixedRate(runnable, 2000);
        try {
            Thread.sleep(10000);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        future.cancel(false);
        System.out.println("执行任务完毕");

二、源码结构

关联关系

image.png

相关文章

  • Java定时任务调度工具详解

    本篇内容:什么是定时任务调度?Java定时任务调度工具详解之 Timer篇Java定时任务调度工具详解之 Quar...

  • Spring Boot 定时任务

    为什么需要定时任务 生活中定时任务的需求越来越多,场景也越来越多 如何实现定时任务 Java自带的java.uti...

  • java定时器

    java定时器 什么是Java定时器?Java 定时器就是在给定的间隔时间执行自己的任务; Java实现定时器有以...

  • Dubbo——时间轮(Time Wheel)算法应用

    定时任务 Netty、Quartz、Kafka 以及 Linux 都有定时任务功能。 JDK 自带的 java.u...

  • java中的定时器

    1、java中普通定时任务 Timer定时器 ScheduledThreadPoolExecutor 定时器 Ti...

  • 一文教你实现Java、Spring动态启停定时任务

    为什么需要定时任务 定时任务的应用场景十分广泛,如定时清理文件、定时生成报表、定时数据同步备份等。 Java定时任...

  • 拥抱Kubernetes,再见了,SpringBoot cron

    项目开发中总是需要执行一些定时任务,比如定时处理数据之后发送邮件,定时更新缓存等等。 Java定时任务 基于 ja...

  • java定时任务

    1.监听类继承ServletContextListener public class MyListen imple...

  • java定时任务

    Java开发过程中经常会遇到使用定时任务的情况,我总结了一下有如下四种方式:Timer、ScheduledExec...

  • Java 定时任务

    在java中一个完整定时任务需要由Timer、TimerTask两个类来配合完成。 Timer类 Timer是一种...

网友评论

      本文标题:JAVA定时任务

      本文链接:https://www.haomeiwen.com/subject/hhprprtx.html