Java Timer(定时器)

Java 多线程6——计时器Timer的使用 + 详细代码模拟实现 + 代码优化 本篇文章带你了解多线程中的计时器,告诉你它的作用,教会你如何使用它,帮你手动代码实现计时器,并手把手带你优化代码,快进来吧! 阅读详情

概述:

Timer是一种定时器工具,用来在一个后台线程计划执行指定任务。它可以安排任务“执行一次”或者定期“执行多次”。                 
 然而在实际的开发过程当中,经常需要一些周期性的操作,比如每5分钟执行某一操作等。
 对于这样的操作最方便、高效的实现方式就是使用java.util.Timer工具类。

方法摘要:

schedule(TimerTask task, Date time) // 安排在 "指定的时间" 执行 指定的任务。(只执行一次)
schedule(TimerTask task,long delay) // 安排在指定延迟后执行指定的任务
schedule(TimerTask task, Date firstTime , long period) // 安排指定的任务在 "指定的时间 " 开始进行 "重复" 的固定延迟执行
schedule(TimerTask task,long delay,long period)// 安排指定的任务指定的延迟后开始进行重复的固定延迟执行.
scheduleAtFixedRate(TimerTask task,Date firstTime,long period)// 安排指定的任务在指定的时间开始进行重复的固定速率执行.
scheduleAtFixedRate(TimerTask task,long delay,long period)//安排指定的任务在指定的延迟后开始进行重复的固定速率执行.
Timer.cancal()// 终止此计时器,丢弃所有当前已安排的任务。
Timer.purge()// 从此计时器的任务队列中移除所有已取消的任务。
TimerTask.cancal()// 把当前任务取消

固定延迟:意味着执行将在最后一次执行开始后的一段时间内开始,即使它被延迟(因此它本身被延迟)。也就是说任务的 下一次执行时间 是相对于 上一次实际执行完成的时间点 ,因此执行时间会不断延后

固定频率:意味着每次执行都将遵守初始计划,无论之前的执行是否被延迟。也就是说任务的 下一次执行时间 是相对于 上一次开始执行的时间点 ,因此执行时间不会延后

关于这两种调度方式,让我们看看如何使用它们:

为了使用固定延迟调度,schedule()方法还有两个重载,每个重载都使用一个额外的参数来表示以毫秒为单位的周期性。为什么两次重载?因为仍然有可能在某个时刻或某个延迟之后开始执行任务。

至于固定频率调度,我们有两个scheduleAtFixedRate()方法,它们的周期也是以毫秒为单位的。同样,我们有一种方法可以在给定的日期和时间启动任务,还有一种方法可以在给定的延迟后启动任务。

注意一点:如果一个任务的执行时间超过了执行周期,那么无论我们使用固定延迟还是固定速率,它都会延迟整个执行链。(固定速率会连续执行,固定延迟会等待延迟再执行)

具体代码

schedule(TimerTask task, Date time)

/**
 * @PROJECT_NAME: demo
 * @DESCRIPTION: 指定时间执行
 */
public class TimerDemo {
    public static void main(String[] args) {
        Calendar ca = Calendar.getInstance();
        System.out.println(ca.getTime());
        ca.set(Calendar.SECOND, ca.get(Calendar.SECOND) + 5);
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println(new Date(scheduledExecutionTime());
            }
        }, ca.getTime());
    }
}

schedule(TimerTask task,long delay)

/**
 * @PROJECT_NAME: demo
 * @DESCRIPTION: 延迟指定时间后执行
 */
public class DelayDemo {
    public static void main(String[] args) {
        System.out.println(new Date());
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println(new Date(scheduledExecutionTime());
            }
        }, 2000);
    }
}

schedule(TimerTask task, Date firstTime, long period)

/**
 * @PROJECT_NAME: demo
 * @DESCRIPTION: 安排指定的任务在 "指定的时间 " 开始进行 "重复" 的固定延迟执行
 */
public class FirstAndPeriodDemo {
    public static void main(String[] args) {
        Calendar ca = Calendar.getInstance();
        System.out.println(ca.getTime());
        ca.set(Calendar.SECOND, ca.get(Calendar.SECOND) + 2);
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(new Date(scheduledExecutionTime()));
            }
        }, ca.getTime(),2000);
    }
}

第一次任务延迟了2s,然后之后因为实现线程执行时间为3s,所以在任务结束时在开始下一次任务。
在这里插入图片描述

schedule(TimerTask task,long delay,long period)


/**
 * @PROJECT_NAME: demo
 * @DESCRIPTION: 安排指定的任务在“指定的延迟”后开始进行“重复”的固定延迟执行
 */
public class DelayAndPeriodDemo {
    public static void main(String[] args) {
        System.out.println(new Date());
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(new Date(scheduledExecutionTime()));
            }
        }, 1000,2000);
    }
}

在这里插入图片描述

scheduleAtFixedRate(TimerTask task,Date firstTime,long period)

/**
 * @PROJECT_NAME: demo
 * @DESCRIPTION:
 */
public class FixAndFirstDemo {
    public static void main(String[] args) {
        System.out.println(new Date());
        new Timer().scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(new Date(scheduledExecutionTime()));
            }
        }, 1000, 2000);
    }
}

按照固定速率执行。
在这里插入图片描述
但是,这儿引出了另外一个问题。既然 Timer 内部是单线程实现的,在执行间隔为2秒、任务实际执行为3秒的情况下, scheduleAtFixedRate 是如何做到2秒输出一次的呢?

【特别注意】

这儿其实是一个障眼法。需要重点关注的是,打印方法输出的值是通过调用 scheduledExecutionTime() 来生成的,而这个方法并不一定是任务真实执行的时间,而是当前任务应该执行的时间。
源码理解

Java定时器Timer的使用 介绍Java定时器Timer的使用 阅读详情

相关推荐

JAVA:定时器的三种方法(详细注解)

第一种: 创建一个thread,然后让它在while循环里一直运行着,通过sleep方法来达到定时任务的效果,代码如下: public class Task1 { public static void main(String[] args) { // run in a second // 每一秒钟执行一次 final long timeInterval = 10...

平凡的人类的博客 7万+

java--Timer 定时器

https://blog.csdn.net/qq_34291570/article/details/94874074? https://blog.csdn.net/u013164931/article/details/80762356? https://blog.csdn.net/hl_java/article/details/79035237? https://blog.csdn.net/xixi_haha123/article/details/81082321? https://blog.csdn.ne

时光里的博客 9656

Java中的定时器Timer

简单来说,定时器就相当于一个“闹钟”,给定时器设定一个任务,约定这个任务在xxx时间之后执行~ Timer类提供了一个核心接口,schedule(安排) 指定一个任务交给定时器,在一定时间之后再去执行这个任务~ 如何实现定时器的效果~ Timer中要包含一个Task类,每个Task就表示一个具体的任务实例,Task里面包含一个时间戳(啥时候执行这个任务),还包含一个Runnable实例(用来表示任务具体是啥)。 Timer里面通过一个带优先级的阻塞队列,来组织如干个task。 这里的优先级是按照时间的先后

wlm123666的博客 716

Java定时器(Timer)

1.介绍 Timer和TimerTask是用于在后台线程中调度任务的java util类。简单地说,TimerTask是要执行的任务,Timer是调度器。 2.调度一次性任务 2.1 指定延迟后执行 让我们从简单地在定时器的帮助下运行单个任务开始: @Test public void givenUsingTimer_whenSchedulingTaskOnce_thenCorrect() { TimerTask task = new TimerTask() { public void

老徐的博客只有干货 2万+

定时器,Timer,java

有的时候我们需要设定一个时间,当到了设定时间的时候让定时器自动去执行某个逻辑,比如qq空间发说说有一个功能就是定时发送。Java标准库中提供了定时器的实现,就是java.util.Timer这个类。

BlueSakuraStar的博客 1097

Java语言 Timer 定时器的四种使用方式

Timer定时器的四种使用方法,代码如下: import java.util.Calendar; import java.util.Date; import java.util.Timer; import java.util.TimerTask; public class TimeTest { public static void main(String[] args) { timer1(); //timer2(); //timer3(); //timer4();

专注基础架构领域 3067

java自带定时器Timer

Timer介绍 Timer是jdk中提供的一个定时器工具,使用的时候会在主线程之外起一个单独的线程执行指定的计划任务,可以指定执行一次或者反复执行多次。   TimerTask是一个实现了Runnable接口的抽象类,代表一个可以被Timer执行的任务。 Timer的使用方法 Timer() 创建一个新的计时器。 Timer(boolean isDaemon) 创建一个新的定时器,其相关线程可以指...

以抱制暴的博客 1454

RK3588s Camera ISP调优详解(一)ISP调试工具及环境配置(附下载途径)

本文主要介绍了rkisp tuner工具的连接与使用,并附上了工具及依赖库的下载地址

zongheng_raobo的博客 6302

Java 定时器的全面解析(Timer)

定时器是一种用于在指定时间或以固定间隔执行任务的工具。为什么需要定时器?提高代码效率和可维护性。实现自动化任务调度。被调度的任务可以按照指定时间执行;一个定时器可以调度多个任务,并按照最初约定的时间执行它们。1.被调度的任务可以按照指定时间执行要实现第一个条件,我们可以创建一个扫描线程来扫描任务列表,检查每个任务是否到达指定的执行时间。如果任务到达了预定的执行时间,就执行相应的代码;如果没有达到预定的执行时间,就不执行任务。2.一个定时器可以调度多个任务,并按照最初约定的时间执行它们。

m0_74750751的博客 1093

2022-8-3----Android10.0(MTK)-----SN_Writer 写入 SN 注意事项

SN_Writer、写入SN

qq_854189624的博客 1万+

反激电源反馈环路设计:UC3842的FB脚与COMP脚接法对比与选型

在开关电源设计中,PWM控制器与光耦的反馈环路接法直接影响系统稳定性与动态响应。UC3842作为经典电流模式控制器,其FB脚和COMP脚分别对应内部误差放大器的输入与输出,决定了环路补偿由内部还是外部承担。FB脚接法利用内部基准和运放,设计简单、成本低,适合小功率适配器;COMP脚接法旁路内部运放,补偿网络外置,动态响应快、精度高,适合中大功率工业电源。理解两种接法的原理与差异,有助于在反激电源设计中优化环路补偿,提升负载调整率和批量一致性。

weixin_31887307的博客 330

java的定时器类Timer

java的定时器类Timer

klz 349

STM32 热敏电阻测量温度

1、硬件 STM32F103C8T6、热敏电阻传感器、OLED。 2、热敏电阻传感器的介绍

weixin_54721788的博客 1万+

用Java实现模拟定时器Timer

java.util.Timer是用于调度定时任务的工具,它提供了一系列内置方法来实现“添加任务”和“取消任务”等核心功能。按照方法的功能可分为“调度任务”和“管理定时器”这两大类。(非守护线程),所有任务都在这个线程上串行执行。

2401_89252636的博客 801

H.264 入门篇 - 09 (帧间预测 - 参考帧列表)

短期和长期的关系和区别:在一个解码器中的 DPB(参考帧缓存)大小总是有限的,所以引入一个问题,什么样的参考帧会被保存到 DPB 中,什么样的参考帧会被移除出去,因此将分为长期和短期参考帧,所谓短期参考帧是这帧不会长期驻留在DPB中,如果参考帧缓存已满或者有其他特定的条件,那么短期参考帧就会被从DPB中移除;与之相对的长期参考帧,只要没有遇到某个指定的条件,遇到某个IDR帧, 遇到了清空整个DPB的指令等等,那么长期参考帧会长期在DPB中,不会因为有新的参考帧而被冲刷掉;(P帧是按照编码顺序)

StephenZhou 5112

目标检测数据集 第059期-基于yolo标注格式的水果李子检测数据集(含免费分享)

李子作为中国三大水果之一,种植历史超 3000 年,在市场中占据重要地位。传统李子种植、施肥、采摘依赖大量人力,存在资源浪费、产量估算不准、采摘时机难把控等问题。此前,农业领域缺乏针对 “未成熟李子” 的专用数据集,现有水果检测数据集多聚焦于成熟果实(如苹果、柑橘),且未覆盖李子种植的复杂自然场景。在使用该数据集进行学术研究时,应遵守相关的学术规范,引用该数据集的来源,尊重数据集创作者的劳动成果。,用于训练改进型模型,解决李子精准检测的核心痛点。

深度学习算法工程师,专注于前沿算法研究与实战应用。在这里,我将分享深度学习领域的实战经验、优质数据集资源以及最新论文解读与精度分析,旨在与广大技术爱好者共同探索人工智能的奥秘,助力技术成长与创新。 778

java定时器timer_Java定时器Timer

Java定时器Timer在JDK库中,Timer类主要负责计划任务的功能,也就是在指定的时开始执行某一个任务。Timer类的主要作用就是设置计划任务,但封装任务的类却是TimerTask类,执行计划任务的代码要放入TimerTask的子类中,因为TimerTask是一个抽象类。下面通过实例说一说,如何实现指定时间执行任务以及实现指定周期执行任务。在指定时间执行方法schedule(TimerTas...

weixin_33634121的博客 373

VCS2018在Ubuntu20 18安装步骤

之前在Ubuntu20上下VCS,花了好几天时间,本来想写博客避免以后重复劳动。但由于各种原因拖了一两个月,这次由于疫情原因,期末考试推迟,所以就再次在Ubuntu18上下了VCS,顺便写博客记录。 以下VCS2018安装方法适用于 Ubuntu20、Ubuntu18,亲测可用。其他Linux发行版没试过,但应该步骤差不多。 文章目录安装包安装步骤1、建立安装路径,更改权限2、运行安装器进行安装3、生成License文件4、设置环境变量5、验证License6、启动lmgrd验证软件是否安装成功dvever

s-stone的博客 3万+

Java中的定时器(Timer)

这个队列可以根据任务的执行时间进行排序,使得时间最早的任务位于队列的前端,即最先要执行的任务。但是此时的wait还没有开始执行,而在这个时候,另一个线程调用了schedule方法,添加了一个新任务,新任务的执行时间为19:45。在上面的说明中,我们可以发现问题出现的原因是:在take和wait的操作之间存在一个时间窗口,在这个时间窗口内,如果有新的任务被添加,那么扫描线程可能会错过这个新任务。此时此刻,新的任务已经插入队列,并且位于队首,但是当前的等待时间仍然是30分钟,导致19:45的任务就被错过了。

m0_62055442的博客 2254

Python实战:打造学生信息管理系统

通过本教程,我们构建了一个简单的学生信息管理系统,实现了基本的添加、删除、修改和查询功能。当然,这个系统还有很多可以改进和扩展的地方,比如添加用户界面、增加数据持久化功能、支持批量操作等。简介:本文将介绍如何使用Python编写一个功能齐全的学生信息管理系统,包括添加、删除、修改和查询学生信息的基本操作。学生信息管理系统是学校日常管理中不可或缺的一部分,它能够帮助管理人员快速、准确地管理学生信息,提高工作效率。为了实现上述功能,我们可以设计一个包含学生信息的字典,并使用函数来封装各个操作。

铭渊老黄 1496

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

Java中Timer定时器的使用

执行结果如下:每秒执行一次。

weixin_45797022的博客 546

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

Java Timer 定时器

本文介绍了 Java 中 Timer 类(计时器)的相关内容。。。

Regino的博客 341

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

深度学习工具包 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")

java Timer定时器的用法

原文:java Timer定时器的用法 代码下载地址:http://www.zuidaima.com/share/1550463283694592.htm 直接运行就可以了,每秒执行一次并且打印当前时间

云淡风轻、仅此一抹 1624

java定时器_拾遗Timer定时器

一 Timer 介绍在开发中我们经常会遇到一些简单定时任务的需求,而不需要量级较重的定时任务就可以采取java定时器;java.util.Timer工具类中的Timer 是定时器,但定时任务写在java.util.TimerTask 中,由 Timer 执行 TimerTask ;Timer 的本质就是线程,构造方法如下public 从源码角度可知,如果这样创建定时器非守护线程,即使主线程运行结...

weixin_39747087的博客 108

Java定时器Quartz和Timer

Java定时器之Quartz 简介 官方说明 Quartz is a richly featured, open source job scheduling library that can be integrated within virtually any Java application - from the smallest stand-alone application to...

weixin_30346033的博客 245

java定时器的使用(Timer)

java定时器的使用(Timer)1、在应用开发中,经常需要一些周期性的操作,比如每5分钟执行某一操作等。对于这样的操作最方便、高效的实现方式就是使用java.util.Timer工具类。private java.util.Timer timer;timer = new Timer(true);timer.schedule(new java.util.TimerTask()

ecjtuxuan的专栏 19万+
上一篇: mysql的日期时间类型及精度问题
下一篇: java Logger.error打印错误异常的详细堆栈信息
86上山了、
博客等级 码龄7年 80粉丝 47原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址