Atitit 定时器timer 总结 目录 1. 定时器 循环定时器 和timeout超时定时器 1 2. Spring定时器 1 2.1. 大概流程 1 2.2. 核心源码springboot 1

Atitit 定时器timer 总结

 

目录

1. 定时器 循环定时器 和timeout超时定时器 1

2. Spring定时器 1

2.1. 大概流程 1

2.2. 核心源码springboot 1

3. Js定时器 window.setInterval 2

4. Java定时器 timer 3

 

 

  1. 定时器 循环定时器 和timeout超时定时器

 

  1. Spring定时器

 

    1. 大概流程

增加一个定时配置类,添加@Configuration和@EnableScheduling注解

使用cron表达式生成器生成一个表达式

定义一个方法,增加Scheduled注解,讲表达式放入即可

运行此springboot项目即可。

 

 

    1. 核心源码springboot

package timer;

 

import org.springframework.context.annotation.Configuration;

import org.springframework.scheduling.annotation.EnableScheduling;

import org.springframework.scheduling.annotation.Scheduled;

 

@Configuration

@EnableScheduling

public class SchedulingConfig {

 

    @Scheduled(cron = "0/5 * * * * ? ")

    public void test(){

        System.out.println("定时调度。。。。。。。。。。。。");

    }

    

}

 

注意,实际的时间他说只能六个参数。。表达式生成器是7个参数,去掉最后一个即可

 

  1. Js定时器 window.setInterval

 

1.循环执行:

var timeid = window.setInterval(“方法名或方法”,“延时”);

window.clearInterval(timeid);

<script type="text/javascript">

  $(document).ready(function(){

    //循环执行,每隔1秒钟执行一次 1000     var t1=window.setInterval(refreshCount, 1000);

    function refreshCount() {

      console.log("ready");

    }

    //去掉定时器的方法  
    window.clearInterval(t1);   
 });
</script>

 

  1. Java定时器 timer

 

 

 16     Timer timer = new Timer();

17     timer.schedule(new TimerTask() {

18       public void run() {

19         System.out.println("-------设定要指定任务--------");

20       }

21     }, 2000);// 设定指定的时间time,此处为2000毫秒

 

 

 

 

Atitit spring 定时器 CRON表达式    含义 

 

原文地址:https://www.cnblogs.com/attilax/p/15197453.html