在定时器的类上添加注解:

@Component
@EnableAsync
@EnableScheduling

一、普通的定时器:

  每天15:10执行的定时器

  @Scheduled(cron="0 10 15 * * ?")
  public void first() throws InterruptedException {
System.out.println("第一个定时任务开始 : " + LocalDateTime.now().toLocalTime() + "\r\n线程 : " + Thread.currentThread().getName());
System.out.println();
Thread.sleep(1000 * 10);
}

二、多线程定时器:

    @Async
@Scheduled(fixedDelay = 2000)
public void second() {
System.out.println("第二个定时任务开始 : " + LocalDateTime.now().toLocalTime() + "\r\n线程 : " + Thread.currentThread().getName());
ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
for (int i = 0; i < 10; i++) {
scheduledThreadPool.schedule(new Runnable() {
public void run() {
System.out.println("delay 3 seconds==========================" + Thread.currentThread().getId() + Thread.currentThread().getName());
}
}, 3, TimeUnit.SECONDS);
}
}
@Scheduled(cron=" ")    使用的cron表达式

推荐一个在线的cron表达式生成器 http://cron.qqe2.com/

cron表达式简单学习 : https://blog.csdn.net/fanrenxiang/article/details/80361582

Springboot中定时器的简单使用的更多相关文章

  1. java中定时器的简单使用

    1.首先肯定是容器一启动就要启动定时器,所以我们可以选择把定时器写在一个监听器里,容器一启动所以监听器也就跟着启动,然后定时器就可以工作了. 第一步,把自己写的监听器加到web.xml中: 第二步,写 ...

  2. springboot 中使用websocket简单例子

    gradle 中添加依赖,引入websocket支持 compile("org.springframework.boot:spring-boot-starter-websocket:${sp ...

  3. Spring-Boot 中 Redis 的简单使用以及简单模糊匹配删除

    https://yulaiz.com/spring-boot-redis-simple/

  4. 7 — 简单了解springboot中的thymeleaf

    1.官网学习地址 https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html 2.什么是thymeleaf? 一张图看明白: 解读: ...

  5. 在SpringBoot中配置定时任务

    前言 之前在spring中使用过定时任务,使用注解的方式配置很方便,在SpringBoot中的配置基本相同,只是原来在spring中的xml文件的一些配置需要改变,在SpringBoot中也非常简单. ...

  6. SpringBoot图文教程5—SpringBoot 中使用Aop

    有天上飞的概念,就要有落地的实现 概念+代码实现是本文的特点,教程将涵盖完整的图文教程,代码案例 文章结尾配套自测面试题,学完技术自我测试更扎实 概念十遍不如代码一遍,朋友,希望你把文中所有的代码案例 ...

  7. SpringBoot图文教程6—SpringBoot中过滤器的使用

    有天上飞的概念,就要有落地的实现 概念十遍不如代码一遍,朋友,希望你把文中所有的代码案例都敲一遍 先赞后看,养成习惯 SpringBoot 图文系列教程技术大纲 鹿老师的Java笔记 SpringBo ...

  8. 在商城系统中使用设计模式----简单工厂模式之在springboot中使用简单工厂模式

    1.前言: 不了解简单工厂模式请先移步:在商城中使用简单工厂.在这里主要是对springboot中使用简单工厂模式进行解析. 2.问题: 什么是简单工厂:它的实现方式是由一个工厂类根据传入的参数,动态 ...

  9. SpringBoot中使用Spring Data Jpa 实现简单的动态查询的两种方法

    软件152 尹以操 首先谢谢大佬的简书文章:http://www.jianshu.com/p/45ad65690e33# 这篇文章中讲的是spring中使用spring data jpa,使用了xml ...

随机推荐

  1. PHPStudyLite启动不成功怎么办

    点击环境端口检测 有端口打开则关闭 一切正常后重新开启

  2. js实现防抖函数和节流函数

    防抖函数(debounce) 含义:防抖函数指的是在特定的时间内没有再次触发,才得以进行接下来的函数运行: 用途:当window.onresize不断的调整大小的时候,为了避免不断的重排与重绘,可以用 ...

  3. Cesium专栏-空间分析之坡向分析(附源码下载)

    Cesium Cesium 是一款面向三维地球和地图的,世界级的JavaScript开源产品.它提供了基于JavaScript语言的开发包,方便用户快速搭建一款零插件的虚拟地球Web应用,并在性能,精 ...

  4. AI-Web1靶机渗透

    先上一张图: 靶机信息及地址:https://www.vulnhub.com/entry/ai-web-1,353/ 下载到靶机后,在VMware里打开,kali使用arp-scan -l 扫到 在浏 ...

  5. java 通过Qrcode生成二维码添加图片logo和文字描述

    /** * 二维码创建 * @author yhzm * */ public class printServiceImpl extends BaseService { public void barC ...

  6. class与class的继承

    class Point{ constructor(x,y){ this.x = x; this.y = y; } toString(){ return '(' + this.x + ',' + thi ...

  7. AcWing 28. 在O(1)时间删除链表结点

    地址 https://www.acwing.com/problem/content/description/85/ 题目描述给定单向链表的一个节点指针,定义一个函数在O(1)时间删除该结点. 假设链表 ...

  8. Spring Cloud 如何搭建eureka

    Eureka Server 的搭建 eureka 是 Spring Cloud 的注册中心,提供服务注册和服务发现的功能. 利用idea 快速创建一个eureka应用File - NewProject ...

  9. [CF1082D]Maximum Diameter Graph

    题目描述 Description Graph constructive problems are back! This time the graph you are asked to build sh ...

  10. WPF DATAGrid 空白列 后台绑定列 处理

    原文:WPF DATAGrid 空白列 后台绑定列 处理 AutoGenerateColumns <DataGrid x:Name="dataGrid" Margin=&qu ...