Java 定时任务-最简单的3种实现方法

 一、Timer

Timer是JAVA自带的定时任务类,实现如下:

public class MyTimerTask {    
    public static void main(String[] args) {        
        // 定义一个任务       
        TimerTask timerTask = new TimerTask() {            
        @Override            
            public void run() {                
            System.out.println("打印当前时间:" + new Date());    
            }       
         };        
        // 计时器       
        Timer timer = new Timer();       
        // 开始执行任务 (延迟1000毫秒执行,每3000毫秒执行一次)        
        timer.schedule(timerTask, 1000, 3000);    
    }
}

Timer 优缺点分析

优点是使用简单,缺点是当添加并执行多个任务时,前面任务的执行用时和异常将影响到后面任务,这边深海建议谨慎使用。

二、ScheduledExecutorService

ScheduledExecutorService 也是Java自带的类,

它可以实现Timer具备的所有功能,并解决了 Timer类存在的问题

实现如下:

public class MyScheduledExecutorService {    
    public static void main(String[] args) {        
        // 创建任务队列   10 为线程数量      
        ScheduledExecutorService scheduledExecutorService = 
                Executors.newScheduledThreadPool(10); 
        // 执行任务      
        scheduledExecutorService.scheduleAtFixedRate(() -> {          
            System.out.println("打印当前时间:" + new Date());      
        }, 1, 3, TimeUnit.SECONDS); // 1s 后开始执行,每 3s 执行一次   

  }
}

ScheduledExecutorService 优缺点分析

优点是,该类是JDK1.5自带的类,使用简单,缺点是该方案仅适用于单机环境。

三、Spring Task

Spring系列框架中Spring Framework自带的定时任务,

使用上面两种方式,很难实现某些特定需求,比如每周一执行某任务,但SpringTask可轻松实现。

以SpringBoot为例来实现:

1、开启定时任务

在SpringBoot的启动类上声明 @EnableScheduling:

@SpringBootApplication
@EnableScheduling //开启定时任务
public class DemoApplication {  
     // --  -- 
}

2、添加定时任务

只需使用@Scheduled注解标注即可,

如果有多个定时任务,可以创建多个@Scheduled标注的方法,示例如下:

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component // 把此类托管给 Spring,不能省略
public class TaskUtils {    
    // 添加定时任务    
    @Scheduled(cron = "30 40 23 0 0 5") // cron表达式:每周一 23:40:30 执行    
    public void doTask(){        
        System.out.println("我是定时任务~");    
    }
}

 Spring Boot 启动后会自动加载并执行定时任务,无需手动操作。

Cron 表达式

Spring Task 的实现需要使用 cron 表达式来声明执行的频率和规则,cron 表达式是由 6 位或者 7 位组成的(最后一位可以省略),每位之间以空格分隔,每位从左到右代表的含义如下:

5bdc076e79dfa867ee79fae9743731e5.png

其中 * 和 ? 号都表示匹配所有的时间。

e941509e3f1ac07ebee74c70f368654f.png

cron 表达式在线生成地址:https://cron.qqe2.com/

知识扩展:分布式定时任务

上面的方法都是关于单机定时任务的实现,如果是分布式环境可以使用 Redis 来实现定时任务。

使用 Redis 实现延迟任务的方法大体可分为两类:通过 ZSet 的方式和键空间通知的方式

1、ZSet 实现方式

通过 ZSet 实现定时任务的思路是,将定时任务存放到 ZSet 集合中,并且将过期时间存储到 ZSet 的 Score 字段中,然后通过一个无线循环来判断当前时间内是否有需要执行的定时任务,如果有则进行执行,具体实现代码如下:

import redis.clients.jedis.Jedis;
import utils.JedisUtils;
import java.time.Instant;
import java.util.Set;
public class DelayQueueExample {        
    private static final String _KEY = "DelayQueueExample";        
    public static void main(String[] args) throws InterruptedException {        
        Jedis jedis = JedisUtils.getJedis();        
        // 30s 后执行        
        long delayTime = Instant.now().plusSeconds(30).getEpochSecond();       
        jedis.zadd(_KEY, delayTime, "order_1");        
        // 继续添加测试数据        
       jedis.zadd(_KEY, Instant.now().plusSeconds(2).getEpochSecond(), "order_2");       
      jedis.zadd(_KEY, Instant.now().plusSeconds(2).getEpochSecond(), "order_3");        
      jedis.zadd(_KEY, Instant.now().plusSeconds(7).getEpochSecond(), "order_4");        
     jedis.zadd(_KEY, Instant.now().plusSeconds(10).getEpochSecond(), "order_5");        
        // 开启定时任务队列        
        doDelayQueue(jedis);    
    }    
    /**     
    * 定时任务队列消费     
    * @param jedis Redis 客户端     
    */    
    public static void doDelayQueue(Jedis jedis) throws InterruptedException {        
        while (true) {            
            // 当前时间            
            Instant nowInstant = Instant.now();            
            long lastSecond = nowInstant.plusSeconds(-1).getEpochSecond(); 
            // 上一秒时间            
            long nowSecond = nowInstant.getEpochSecond();            
            // 查询当前时间的所有任务            
            Set data = jedis.zrangeByScore(_KEY, lastSecond, nowSecond);            
            for (String item : data) {                
            // 消费任务                
            System.out.println("消费:" + item);            
        }            
        // 删除已经执行的任务            
        jedis.zremrangeByScore(_KEY, lastSecond, nowSecond);            
        Thread.sleep(1000); // 每秒查询一次        
        }    
    }
}

2、键空间通知

我们可以通过 Redis 的键空间通知来实现定时任务,它的实现思路是给所有的定时任务设置一个过期时间,等到了过期之后,我们通过订阅过期消息就能感知到定时任务需要被执行了,此时我们执行定时任务即可。

默认情况下 Redis 是不开启键空间通知的,需要我们通过 config set notify-keyspace-events Ex 的命令手动开启,开启之后定时任务的代码如下:

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPubSub;
import utils.JedisUtils;
public class TaskExample {    
    public static final String _TOPIC = "__keyevent@0__:expired"; // 订阅频道名称   
    public static void main(String[] args) {       
        Jedis jedis = JedisUtils.getJedis();       
        // 执行定时任务        
        doTask(jedis);    
    }   
     /**     
       * 订阅过期消息,执行定时任务     
       * @param jedis Redis 客户端     
       */    
    public static void doTask(Jedis jedis) {        
        // 订阅过期消息        
        jedis.psubscribe(new JedisPubSub() {            
            @Override            
 public void onPMessage(String pattern, String channel, String message) {                
            // 接收到消息,执行定时任务                
            System.out.println("收到消息:" + message);            
            }            
        }, _TOPIC);    
    }
}
  • 113
    点赞
  • 740
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
Java中有多方式可以实现定时任务。其中比较常用的两方式是使用Timer和TimerTask类,以及使用ScheduledThreadPoolExecutor类。 1. 使用Timer和TimerTask类: Timer和TimerTask是Java中用于定时任务的类。Timer负责设定TimerTask的起始与间隔执行时间,而TimerTask是一个抽象类,需要实现自己的run方法,并通过Timer进行执行。下面是一个示例代码: ```java import java.time.LocalDateTime; import java.util.Timer; import java.util.TimerTask; public class Schedule { public static void main(String[] args) { TimerTask timerTask = new TimerTask() { @Override public void run() { System.out.println("当前线程:" + Thread.currentThread().getName() + " 当前时间:" + LocalDateTime.now()); } }; // 在指定延迟0毫秒后开始,随后每2000毫秒间隔执行timerTask new Timer().schedule(timerTask, 0L, 2000L); System.out.println("当前线程:" + Thread.currentThread().getName() + " 当前时间:" + LocalDateTime.now()); } } ``` 在上面的示例中,创建了一个TimerTask对象,并实现了run方法来定义定时任务的逻辑。然后通过Timer的schedule方法来指定任务的延迟执行时间和间隔执行时间。 2. 使用ScheduledThreadPoolExecutor类: Java 5.0引入的java.util.concurrent包中的ScheduledThreadPoolExecutor类可以实现更灵活的定时任务。它是一个线程池,用于以给定的速率或延迟重复执行任务。相比于Timer和TimerTask的组合,ScheduledThreadPoolExecutor允许多个服务线程,并且不需要子类TimerTask(只需实现Runnable接口)。下面是一个示例代码: ```java import java.time.LocalDateTime; import java.util.concurrent.*; public class Schedule { public static void main(String[] args) { // 创建一个ScheduledThreadPoolExecutor线程池,心线程数为5 ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(5); // 创建Runnable打印当前线程和当前时间 Runnable r = () -> System.out.println("当前线程:" + Thread.currentThread().getName() + " 当前时间:" + LocalDateTime.now()); /** * schedule:只执行一次调度 * scheduleAtFixedRate:一开始就计算间隔时间,如果任务超过间隔时间,那么就直接开始下一个任务 * scheduleWithFixedDelay:任务无论执行多久,都要等待上一轮任务完成之后再间隔指定时间,然后才开始下一个任务 */ // 在指定1秒延迟后执行r,之后每两秒执行一次 scheduledExecutorService.scheduleAtFixedRate(r, 1, 2, TimeUnit.SECONDS); } } ``` 在上面的示例中,首先创建了一个ScheduledThreadPoolExecutor线程池,核心线程数为5。然后创建一个Runnable对象,用于定义定时任务的逻辑。最后通过scheduleAtFixedRate方法来指定任务的延迟执行时间和间隔执行时间。 综上所述,Java中可以使用Timer和TimerTask类,以及ScheduledThreadPoolExecutor类来实现定时任务。选择哪方式取决于具体的需求和场景。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值