Java定时注解@Scheduled的使用,fixedDelay,fixedRate,cron的使用

java 定时任务 @Scheduled 定义在方法上,让方法按照表达式约束的时间自动触发 1.1、需搭建好spring环境 1.2、使用的时候,将其注解带上表达式写在方法上,注意类上也是需要注解的(@Service/@Component) 2、关于表达式: 2.1、语法【,】表示不连续的值,如“0,5,10”--》意味着0秒,5秒,10秒的时间【-】表示连续的值,如“0-5”-》意味着0秒,1秒,2秒,3秒,4秒,5秒的时间【*】表示任意值,如:*每秒钟【/】表示字符用于指定增量,如:“0/5”-》每隔5秒分 【, 阅读详情
AI摘要

本文详细介绍了Spring框架中@Scheduled注解的使用,包括fixedDelay、fixedRate和cron表达式,提供了一系列示例及说明,帮助开发者理解和配置定时任务。同时,文章还提及了定时任务启用的关键配置@EnableScheduling,并分享了作者的心得体会。

Java定时注解@Scheduled的使用,fixedDelay,fixedRate,cron的使用

问题背景

项目中经常使用定时任务,spring提供了定时注解,很方便就可以使用
注意事项

参数简介

1 一个cron表达式有至少6个(也可能7个),使用空格分隔时间元素
按顺序依次为:[秒] [分] [小时] [日] [月] [周] [年]

  • 秒(0~59)
  • 分钟(0~59)
  • 小时(0~23)
  • 天(月)(0~31,但是你需要考虑你月的天数)
  • 月(0~11)
  • 天(星期)(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT)
  • 年份(1970-2099)

2 其中每个元素可以是一个值(如6),一个连续区间(9-12),一个间隔时间(8-18/4)(/表示每隔4小时),一个列表(1,3,5),通配符。由于"月份中的日期"和"星期中的日期"这两个元素互斥的,必须要对其中一个设置
设置用例:

  • */5 * * * * ?   每五秒一次
  • 0 */5 * * * ?   每五分一次
  • 0 0 10,14,16 * * ?   每天上午10点,下午2点,4点
  • 0 0/30 9-17 * * ?   朝九晚五工作时间内每半小时
  • 0 0 12 ? * WED   表示每个星期三中午12点
  • 0 0 12 * * ?   每天中午12点触发
  • 0 15 10 ? * *   每天上午10:15触发
  • 0 15 10 * * ?   每天上午10:15触发
  • 0 15 10 * * ? *   每天上午10:15触发
  • 0 15 10 * * ? 2005   2005年的每天上午10:15触发
  • 0 * 14 * * ?   在每天下午2点到下午2:59期间的每1分钟触发
  • 0 0/5 14 * * ?   在每天下午2点到下午2:55期间的每5分钟触发
  • 0 0/5 14,18 * * ?   在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
  • 0 0-5 14 * * ?   在每天下午2点到下午2:05期间的每1分钟触发
  • 0 10,44 14 ? 3 WED   每年三月的星期三的下午2:10和2:44触发
  • 0 15 10 ? * MON-FRI   周一至周五的上午10:15触发
  • 0 15 10 15 * ?   每月15日上午10:15触发
  • 0 15 10 L * ?   每月最后一日的上午10:15触发
  • 0 15 10 ? * 6L   每月的最后一个星期五上午10:15触发
  • 0 15 10 ? * 6L 2002-2005   2002年至2005年的每月的最后一个星期五上午10:15触发
  • 0 15 10 ? * 6#3   每月的第三个星期五上午10:15触发

3 表达式能包含一些范围或列表

  • 子表达式(天(星期))可以为 “MON-FRI”,“MON,WED,FRI”,“MON-WED,SAT”

4 “*” 字符代表所有可能的值

  • “*” 在子表达式(月)里表示每个月的含义,“*”在子表达式(天(星期))表示星期的每一天

5 “/”字符用来指定数值的增量

  • 在子表达式(分钟)里的“0/15”表示从第0分钟开始,每15分钟
  • 在子表达式(分钟)里的“3/20”表示从第3分钟开始,每20分钟(它和“3,23,43”)的含义一样

6 “?”字符仅被用于天(月)和天(星期)两个子表达式,表示不指定值

  • 当2个子表达式其中之一被指定了值以后,为了避免冲突,需要将另一个子表达式的值设为“?”

7 “L” 字符仅被用于天(月)和天(星期)两个子表达式,它是单词“last”的缩写,它在两个子表达式里的含义是不同的

  • 在天(月)子表达式中,“L”表示一个月的最后一天
  • 在天(星期)自表达式中,“L”表示一个星期的最后一天,也就是SAT
    8 如果在“L”前有具体的内容,它就具有其他的含义了
  • “6L”表示这个月的倒数第6天,“FRIL”表示这个月的最一个星期五
  • 在使用“L”参数时,不要指定列表或范围,因为这会导致问题
字段允许值允许的特殊字符
秒0-59, - * /
分0-59, - * /
小时0-23, - * /
日期1-31, - * ? / L W C
月份1-12 或者 JAN-DEC, - * /
星期1-7 或者 SUN-SAT, - * ? / L C #
年(可选)留空, 1970-2099, - * /

9 默认使用服务器默认时区

  • 可以设置为java.util.TimeZone中的zoneId, 每天零点执行@Scheduled(cron = “0 0 0 * * ?”, zone = “Asia/Shanghai”)

10 fixedDelay

  • @Scheduled(fixedDelay = 5000) //上一次执行完毕时间点之后5秒再执行

11 fixedDelayString

  • 与 fixedDelay 意思相同,只是使用字符串的形式,唯一不同的是支持占位符
  • @Scheduled(fixedDelayString = “5000”) //上一次执行完毕时间点之后5秒再执行

12 fixedRate

  • @Scheduled(fixedRate = 5000) //上一次开始执行时间点之后5秒再执行

13 fixedRateString

  • 与 fixedRate 意思相同,只是使用字符串的形式,唯一不同的是支持占位符

14 initialDelay

  • @Scheduled(initialDelay=1000, fixedRate=5000) //第一次延迟1秒后执行,之后按fixedRate的规则每5秒执行一次

15 initialDelayString

  • initialDelay 的字符串String格式,initialDelay 意思相同,只是使用字符串的形式,唯一不同的是支持占位符

16 1 需要使能@Scheduled注解,在类前面添加@EnableScheduling进行使能,否则定时注解默认无效

项目创建

1 需要使能@Scheduled注解,在类前面添加@EnableScheduling进行使能,否则定时注解默认无效

package com.yg.schedule.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

/**
 * @Author suolong
 * @Date 2022/3/21 14:14
 * @Version 1.0
 */
@Service
@EnableScheduling
@Slf4j
public class ScheduleService {

    @Scheduled(fixedDelay = 1*1000) //毫秒为单位,该函数全部完成之后,暂停一秒开始执行第二次
    public void fixedDelayTest(){
        log.info("FixedDelay test");
    }

    @Scheduled(fixedRate = 1*1000) //毫秒为单位,调用一次函数,马上开始计时一秒,然后执行第二次
    public void fixedRateTest(){
        log.info("FixedRate test");
    }

    //cron语法:[秒] [分] [小时] [日] [月] [周] [年]
    //每隔一秒执行一次
    @Scheduled(cron = "0/1 * * * * *")
    public void cronTest(){
        log.info("Cron test");
    }
}

2 pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.yg</groupId>
    <artifactId>schedule</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>schedule</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

3 启动类

package com.yg.schedule;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ScheduleApplication {

    public static void main(String[] args) {
        SpringApplication.run(ScheduleApplication.class, args);
    }

}

4 整体项目目录

测试结果

1 fixedDelay和fixedRate测试

2 cron测试

心得

  • 定时任务使用这些应该是足够了




作为程序员第 81 篇文章,每次写一句歌词记录一下,看看人生有几首歌的时间,wahahaha …

Lyric:咸咸的汗水

@Scheduled定时器 当任务开始时Spring就会给这个任务计时,5秒钟时候Spring就会再次调用这个任务,可是发现原来的任务还在执行,这个时候第二个任务就阻塞了(这里只考虑单线程的情况下,多线程后面再讲),甚至如果第一个任务花费的时间过长,还可能会使第三第四个任务被阻塞。每分钟的第十秒执行一次。比如第一次执行时间为2023-10-20 23:09:10,下一次2023-10-20 23:10:10,再下一次2023-10-20 23:11:10。上一次任务执行完毕时间点之后多长时间再执行,任务的执行要等上一个任务执行结束。 阅读详情

相关推荐

Spring定时任务 - @Schedule注解详解

一、@Schedule注解一览 @Scheduled注解共有8个属性(其中有3组只是不同类型的相同配置)和一个常量CRON_DISABLED,源码如下: /** * 标记要调度的方法的注释。 必须准确指定 cron、fixedDelay 或 fixedRate 属性之一。带注释的方法必须没有参数。 * 它通常有一个 void 返回类型; 如果不是,则在通过调度程序调用时将忽略返回值。@Scheduled 注解的处理是通过注册 * 一个 ScheduledAnnotationBeanPostPr

混沌世界 1万+

spring的定时任务@Scheduled中fixedDelay、fixedRate、initialDelay 和cron表达式的解析及区别

@Scheduled注解可以控制方法定时执行,其中有三个参数可选择: 1、fixedDelay控制方法执行的间隔时间,是以上一次方法执行完开始算起,如上一次方法执行阻塞住了,那么直到上一次执行完,并间隔给定的时间后,执行下一次。 2、fixedRate是按照一定的速率执行,是从上一次方法执行开始的时间算起,如果上一次方法阻塞住了,下一次也是不会执行,但是在阻塞这段时间内累计应该执行的次数,当不...

wangfei 1万+

@scheduled注解作用

用来开启定时任务fixedRate 任务两次执行时间间隔是任务的开始点,而 fixedDelay 的间隔是前次任务的结束与下次任务的开始。@Scheduled(fixedRate = 5000) 5秒执行一次cron表达式详解一个cron表达式有至少6个(也可能7个)有空格分隔的时间元素。按顺序依次为:秒(0~59) 分钟(0~59) 3 小时(0~23) 4 天(0~31) 5 月(0~11) 6 星期(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT) 年份(1970-20

Jack章臣的博客 517

Java中使用@Scheduled定时器操作

在Java中使用@Scheduled定时器的几种种操作方式。 1、fixedRate 例:@Scheduled(fixedRate = 5000) //上一次开始执行时间点之后5秒再执行 2、fixedDelay 例:@Scheduled(fixedDelay = 5000) //上一次执行完毕时间点之后5秒再执行 3、cron 例:@Scheduled(cron = "0 0/1 9-21 * * ?")//每天早上9点-晚上21点间执行,每次执行间隔一分钟 着重讲一下cron:

suxuelian的博客 2万+

在AutoDL的A100云服务器上,用Docker搞定Sapien+RobotWin环境(附完整conda list)

本文详细介绍了在AutoDL的A100云服务器上使用Docker构建Sapien+RobotWin环境的完整指南。通过配置Vulkan渲染管线、精确复现Conda环境以及解决常见问题,帮助研究人员高效搭建稳定的机器人仿真开发环境,特别适合需要高性能GPU加速的复杂仿真任务。

weixin_30689307的博客 438

Stable-Baselines3性能优化:多环境训练对比指南(DummyVecEnv vs SubprocVecEnv)

本文深入对比了Stable-Baselines3中DummyVecEnv与SubprocVecEnv两种向量化环境的性能差异与应用场景。DummyVecEnv适用于轻量级、步进快的环境,调试友好且开销低;而SubprocVecEnv则通过真正的多进程并行,能显著加速计算密集型环境的训练。文章通过基准测试与决策树,指导开发者根据环境计算成本、CPU核心数等关键因素做出最优选择,以实现训练效率最大化。

weixin_29063979的博客 153

@Scheduled注解

@Scheduled 定时器

Hai_Dreamer的博客 1万+

Android投屏开发实战:手把手教你用Miracast实现Sink端功能(附完整代码)

本文是一份Android投屏Sink端开发的实战指南,详细讲解了如何利用Miracast协议和Wi-Fi P2P技术,从零构建一个功能完整的屏幕接收器。内容涵盖环境搭建、通过反射启用WFD Sink模式、RTSP协议协商、RTP流接收与解析,以及最终使用IjkPlayer进行音视频解码播放的全过程,并附有核心代码示例。

cherry的博客 893

Spring使用@Scheduled注解的参数详解

比如设置了initialDelay=10000和fixedRate=5000的话,那么在应用启动后,任务会在10秒后第一次执行,之后每5秒执行一次。这里要注意的是,如果上一个任务执行时间超过了固定的间隔时间,新的任务会等到上一个任务执行完毕后再启动。一般来说,固定时间的任务可以使用fixedRate和fixedDelay,复杂的定时策略可以利用cron表达式。在现代Java开发中,定时任务是一个常见的需求。”和星号“*”的使用是有规则的,星号代表任何时间的意思,而问号则用在日期或者星期字段上,表示不指定。

分享AI大模型相关前沿技术、最新人工智能资讯 1058

点云直线检测-3DLineDetection复现

点云直线分割-Fast 3D line detection

阁楼 8167

hdu 1195 Open the Lock(bbfs(双向广搜))

<br />第一次使用双向广搜算法求解,感觉效率的确提高了不止两倍。刚开始的一直WA,后来发现是每次都要扩展一层的节点,然后在扩展另一层的节点,而不是每次只扩展一个节点。<br /> /* Author: ACb0y Date: 2010-9-21 Type: bbfs(双向广搜) ProblemId: hdu 1195 Open the Lock Result: AC */ #include <iostream> #include <queue> using namespace

个人独立博客http://www.acb0y.cn/ 1531

Java Spring定时任务 亲测

Java Spring定时任务, @Scheduled常用参数简单代码实例开启定时任务定时任务触发方法 1Spring定时任务有两种配置方式 注解方式, Xml方式本文讲解 @Scheduled的几个常用参数cron元素说明:通配符说明:定时任务触发方法 2 简单代码实例 开启定时任务 package com.test.web.config; import org.springframewor...

Shawn Jeon`s blog 998

【AutoSAR CAN】04 - 如何使用Davinci Configurator Pro工具配置CAN的波特率

改参数为Tx Arbitration start delay value ,可用于延迟仲裁过程的起始点,用来补偿CAN收发器及线路的传输延迟,以优化传输性能。有关详细信息,请参阅硬件手册。该参数指定采样点模式,即选择多少个采样点的数量,如果选择了每比特三个样本选项,则该点标记第三个样本的位置;我们要配置CAN的波特率,其实就是要弄明白 CAN 控制器的位时序即可,具体的理论部分和EB的配置部门可参考文章《该参数表示传播延迟有多少个时间单位Tq,即上文中的Propagation delay/PROGSEG。.

2612

@Scheduled()注解以及cron表达式详解

@Schedule()注解以及cron表达式详解 @Schedule注解:任务自动化调度。 1. @Scheduled(fixedDelay = 5000) 延迟执行。任务在上个任务完成后达到设置的延时时间就执行。 此处,任务会在上个任务完成后经过5s再执行。 2. @Scheduled(fixedRate = 5000) 定时执行。任务间隔规定时间即执行。 此处,任务每隔五秒便会执行一次。 3. @Scheduled(cron = “0 0 2 * * ?”) 自定义规则执行。比较复杂,一般用6或7个标识

qq_37807821的博客 7万+

2025华为OD 面试手撕代码真题【字符串至多删除一个字符,是否能构成回文串】

2025华为OD 面试手撕代码真题【字符串至多删除一个字符,是否能构成回文串】

MISAYAONE的博客 1725

UG NX二次开发(C#)-外部模式-批量删除某些图层中的体对象和曲线对象

在 UG NX中,有时需要批量删除某文件夹下多个.prt文件中固定一些图层中的体对象和曲线对象,这时如果采用手动逐个打开模型,并选择针对的图层删除,即耗时也容易遗漏,因此采用外部模式来处理比较方便。本文介绍批量删除某些图层中的体对象和曲线对象。

GimiGimmy 3041

spring的定时任务@Scheduled简单实用

方式一:使用注解 @Component @EnableScheduling//可以在启动类上注解也可以在当前文件 public class TestJob { @Scheduled(cron = "0/10 * * * * ?") public void runfirst(){ System.out.println("********first job is ...

qq_41899174的博客 5万+

InTheHand.Net.Personal+BlueTooth4,蓝牙开发

项目中有蓝牙开发的必备dll,现在网上相关的文档博客不少,有的说的多有的说少,但是却很少有这两个dll,分享一下。

Java中@Scheduled定时任务详解

Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义,Cron有如下两种语法格式: (1) Seconds Minutes Hours DayofMonth Month DayofWeek Year (2)Seconds Minutes Hours DayofMonth Month DayofWeek 一、结构 corn从左到右(用空格隔开): 秒 分 小时 月份中的日期 月份 星期中的日期 年份 二、各字段的含义 注意事项: 每一个域都使用数字,但还可以出现如下特殊

筱筱攻程师的博客 1万+

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

Java 定时任务@Scheduled使用

一,注解@Scheduled 1、fixedRate配置了上一次任务的开始时间到下一次任务的开始时间的间隔,每次任务都会执行; 示例: @Scheduled(fixedRate = 5 * 60 * 1000) 任务A开始之后再过5分钟执行任务B 2、fixedDelay配置了上一次任务的结束时间到下一次任务的开始时间的间隔,每次任务都会执行; 示例: @Scheduled(fixedDelay = 10 * 60 * 1000) 任务A结束之后再过10分钟执行任务B 3、cron表达式配置

qq_36636312的博客 6497

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

一文读懂SpringBoot定时任务

虽然面试套路众多,但对于技术面试来说,主要还是考察一个人的技术能力和沟通能力。不同类型的面试官根据自身的理解问的问题也不尽相同,没有规律可循。上面提到的关于这些JAVA基础、三大框架、项目经验、并发编程、JVM及调优、网络、设计模式、spring+mybatis源码解读、Mysql调优、分布式监控、消息队列、分布式存储等等面试题笔记及资料。

2401_85117779的博客 1480

@Scheduled笔记240124

属性:使用Cron表达式定义任务的执行时间,可以实现高度灵活的调度。属性:任务完成后,等待一段固定的时间再执行下一次。注:星期日为每周第一天,所以1-7表示周末到周六。属性:在应用启动后,首次执行任务前的延迟时间。// 启动后等待3秒,然后每隔5秒执行一次。// 任务执行完后,等待5秒再执行下一次。1-7或SUN-SAT(大小写均可)属性:根据固定的频率执行任务。秒 分 时 日 月 周 年。秒 分 时 日 月 周 年。// 每隔5秒执行一次。// 每天凌晨2点执行。

kfepiza的博客 1267

java task 定时任务,定时任务 - Spring task 介绍及使用

开篇在日常的业务开发过程中,可能会需要实现一些周期性定时任务,比如定时同步数据库、定时发送短信 或者 邮件等。那么在 Java 中,共有三种定时任务的实现方式:通过 java.util.Timer、TimerTask 实现。通过 Spring 自带的 SpringTask。(Spring 3.0 以后)通过 Spring 结合 Quartz 实现。而本文说的是第二种方式 - Spring 定时器。...

weixin_31807383的博客 2092

@Scheduled注解各参数详解

@Scheduled注解各参数详解

weixin_50993955的博客 1836

Spring Boot @Scheduled 注解实现定时任务(附完整代码)

在日常开发中,定时任务是高频需求 —— 比如定时清理系统日志、定时同步数据、定时推送通知等。Spring Boot 提供了非常简洁的定时任务实现方式,无需额外引入复杂框架,仅通过两个核心注解就能快速搭建。本文将手把手教你实现一个每 5 秒执行一次的定时器,包含完整代码、配置细节、效果演示和常见问题排查,零基础也能轻松掌握!编写带@Component和@Scheduled的定时器类;主启动类添加@EnableScheduling开启定时功能;启动项目验证效果。

2402_85302906的博客 1073

SpringBoot2.1.X 定时任务

需求: 支付流水账.... (1) 日报表:凌晨1点统计昨天的数据 (2) 月报表:每月1日统计上个月的数据 那么如何在每日凌晨1点或每月1日定时执行一个任务呢? 介绍定时任务实现的几种方式: 1、常见定时任务 Java自带的java.util.Timer类 timer:配置比较麻烦,时间延后问题 timertask:不推荐 2、Quar...

分享我的点点滴滴,在成长路上与你同行! 534
上一篇: Java调用linux指令工具类,直接执行cmd,执行grep指令返回结果,执行sed追加指令,hdfs下载指令,获取文件行数
下一篇: Java调用Hbase出现java.lang.reflect.InvocationTargetException异常解决方案
时间是一种解药
博客等级 码龄10年 119粉丝 252原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值