Java定时任务

Java定时任务--Scheduled


 

缘由:

  定时任务其实能做很多事情,日常签到,提醒,定时获取数据.....。之前就有写定时任务的想法,直到现在才开始付出行动。第一阶段准备写的有:某某平台的签到,和某某平台的数据获取。后续可能会写默认点餐之类的吧(经常忘记点晚饭。。。)

开始入手:

  定时任务最简单的办法就是sleep,之前有看到一个段子,“如何获取一天后的时间:Thread.sleep(1000*60*60*24);”  

  最后选择了SpringBoot+@Scheduled注解的方式,该方式通过Cron表达式设置时间(自行了解),使用起来还是很简单的。

 

Do it

* 创建Maven项目

  我使用的编辑器是IDEA,创建过程就省略了,默认大家都是可以的。

* 首先是创建Application.java
package kylin;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling        //开启自动任务配置
public class Application {
  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}
* 创建定时任务
  @Scheduled(cron="0/5 * *  * * ? ")    //每5秒执行一次
  public void execute(){
    System.out.println("*********任务每5秒执行一次进入测试");
  }

  这里要注意,该方法的类要加上@Component注解

* 附加

  下面是pom.xml(部分),仅供参考

<!-- 继承父包 -->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
  </parent>


  <dependencies>
    <!-- spring-boot使用jetty容器配置begin -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <!-- 排除默认的tomcat,引入jetty容器. -->
      <exclusions>
        <exclusion>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <!-- jetty 容器. -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-jetty</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
    <!-- spring-boot使用jetty容器配置end -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

 

 

以上

定时任务到此结束

注意点1:Application.java文件要增加@EnableScheduling 注解,表示开启自动任务

注意点2:@Scheduled(cron="0/5 * *  * * ? ") ,其中cron表达式可以实现多种组合,具体的可以查看其它相关教程

 

 

如有疑问,欢迎留言

 

转载于:https://www.cnblogs.com/yishilin/p/11127776.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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类来实现定时任务。选择哪种方式取决于具体的需求和场景。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值