首页 Java java教程 JDK Timer定时器的用法

JDK Timer定时器的用法

Jun 28, 2017 am 10:34 AM
jdk timer

Implementing and scheduling a task to be executed by a timer
1) Implement a custom subclass of TimerTask. The run method contains the code that performs the task.
    class RemindTask extends TimerTask {
        public void run() {
            System.out.println("Time up!");
            System.exit(0);
        }
    }
2) Create a thread by instantiating the Timer class
    Timer timer = new timer();
3) Instantiate the timer task object(new RemindTask())
    RemindTask task = new RemindTask();
4) Schedule the timer task for execution.
  (1) execute the task after special milliseconds delay.
    timer.schedule(task,5*1000);
  (2) specify the time when the task suould execute.
    //execute the task at 11:01 p.m
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY,23);
    calendar.set(Calendar.MINUTE,1);
    calendar.set(Calendar.SECOND,0);
    Date time = calendar.getTime();

    timer.schedule(task,time);

Stopping Timer Threads
  By defaulst, aprogram keeps running as long as its timer threads are running. There is four ways to terminate a timer thread
  1) Invoke cancel on the timer.(timer.cancel())
  2) Make the timer's thread a daemon(后台), by creating the timer like this: new Timer(true). If the only threads left in the program are daemon threads, the program exits.
  3) After all the timer scheduleds tasks have finished executing,remove all references to the Timer object. the timer thread will terminate.
  4) Invoke the System.exit method, which makes the entire program and all its threads exit.

Performing a Task repeatedly
There is four Timer method to perform a task repeatedly
  * schedule(TimerTask task, long delay, long period)
    Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay. Subsequent executions take place at approximately regular intervals separated by the specified period.
    执行重复的任务,第一次在延时时间后执行,往后的以特定的时间间隔执行
    timer.schedule(new RemindTask(),3*1000,1*1000)
    RemindTask任务将会在3秒后执行,以后将会以1秒的间隔重复执行

  * schedule(TimerTask task, Date time, long period)
    执行重复的任务,第一次在特定的时间执行,往后的以特定的时间间隔执行

  * scheduleAtFixedRate(TimerTask task, long delay, long period)
    Schedules the specified task for repeated fixed-rate execution, beginning after the specified delay. Subsequent executions take place at approximately regular intervals, separated by the specified period.
    以固定的延时执行重复的任务,首次执行在特定的延时之后,以后的执行发生在特定的时间间隔之后
    temer.scheduleAtFixedRate(new RemindTask(),3*1000,1*1000)
  * scheduleAtFixedRate(TimerTask task, Date firstTime, long period)
    执行重复的任务,第一次在特定的时间执行,往后的以特定的时间间隔执行

  schedule和scheduleAtFixedRate的区别在于,schedule以固定的相对时间间隔执行,如果某一次执行被延时了,往后的执行的执行时间也会相对延时;而scheduleAtFixedRate是以绝对的时间间隔执行,如果某一次执行被延时,它的后一次执行的延时将会缩短。

以上是JDK Timer定时器的用法的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它们
4 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

如何在 Windows 11 上安装 Java 如何在 Windows 11 上安装 Java Apr 13, 2023 pm 09:22 PM

尽管每隔一段时间就会出现大量应用程序,但 Java 仍然是迄今为止最常用和最重要的编程语言之一。许多应用程序依赖于 Windows 操作系统上的 Java,更新它意味着通过为 Java 应用程序提供安全运行的稳定性和安全性来提高性能。您还可以在 Linux 和 macOS 平台上安装 Java。唯一的区别是每个平台的包/文件。现在,有了 Windows 11,是时候下载 Java,在今天的文章中,我们将带您完成在您的设备上安装它的简单步骤。我应该下载哪个版本的 Java?您下载的 Java 版本

oracle数据库需要jdk吗 oracle数据库需要jdk吗 Jun 05, 2023 pm 05:06 PM

oracle数据库需要jdk,其原因是:1、当使用特定的软件或功能时需要包含在JDK中的其他软件或库;2、需要安装Java JDK才能在Oracle数据库中运行Java程序;3、JDK提供了开发和编译Java应用程序的功能;4、满足Oracle对Java函数的要求,以帮助实现和实现特定功能。

java之JDK动态代理实例分析 java之JDK动态代理实例分析 Apr 30, 2023 pm 01:16 PM

1、说明Java中提供了一个动态代理类Proxy,Proxy并不是我们所说的代理对象的类,而是提供了一个创建代理对象的静态方法(newProxyInstance)来获取代理对象。2、实例publicclassHelloWorld{publicstaticvoidmain(String[]args){//获取代理对象ProxyFactoryfactory=newProxyFactory();SellTicketsproxyObject=factory.getProxyObject();proxyO

深度Linux系统安装JDK教程 深度Linux系统安装JDK教程 Feb 15, 2024 pm 12:36 PM

深度Linux系统是一款基于Linux内核的国产操作系统,具有稳定、安全、易用等特点,在深度Linux系统中,安装JDK(JavaDevelopmentKit)是开发Java应用程序的必要步骤,本文将详细介绍如何在深度Linux系统中安装JDK。安装步骤打开深度Linux系统的终端。使用命令行下载JDK安装包,命令如下:```shellsudoapt-getinstallopenjdk-11-jdk```等待下载完成后,系统会自动安装JDK。验证JDK是否安装成功,输入以下命令:```javaj

linux jdk目录在哪 linux jdk目录在哪 Mar 22, 2023 am 09:52 AM

linux jdk目录在bin目录下,具体查找方法是:1、通过“whereis java”命令找到javad的执行目录;2、通过执行文件找到链接文件;3、通过“ls -lrt /etc/alternatives/java”命令找到安装目录即可。

jdk怎么安装-jdk安装教程 jdk怎么安装-jdk安装教程 Mar 04, 2024 pm 05:10 PM

近期有很多小伙伴咨询小编jdk怎么安装,接下来就让我们一起学习一下jdk怎么安装的全部内容吧,希望可以帮助到大家。1、首先下载JDK安装文件,进入JDK安装界面,如图所示。2、单击“下一步”按钮,进入JDK自定义安装界面,如图所示。3、建议选择直接安装到默认目录,单击“下一步”按钮即可进行安装,如图所示。也可以单击“更改”按钮,自行选择安装目录。4、安装完毕后,弹出界面,单击“关闭”按钮即可,如图所示。上面就是小编为大家带来的jdk怎么安装的全部教程,希望对大家能够有所帮助哦。

Linux系统中jdk环境怎么配置 Linux系统中jdk环境怎么配置 May 12, 2023 am 09:31 AM

如下操作步骤为linux系统中部署jdk环境1.下载jdk安装包2.新建安装jdk文件夹(/usr/local/java/jdk)cd/usr/localmkdir/usr/local/javalocal目录下新建java目录cd/usr/local/javamkdir/usr/local/java/jdk新建jdk目录cd/usr/local/java/jdk3.上传jdk安装包到linux4.解压jdk安装包解压格式:tarzxvf压缩包名称后面的名称可以只输入一个首字母,然后使用Tab按键

多jdk环境下如何指定springboot外部配置文件 多jdk环境下如何指定springboot外部配置文件 May 11, 2023 pm 04:01 PM

问题描述当部署环境中存在多个jdk,且默认jdk版本低于jdk8。我们在部署springboot应用的时候,需要指定jdk为jdk8或其以上版本。随之而来的会出现一个问题:springboot应用的外部化配置文件无法加载,它会一直采用的打入应用jar的默认的配置文件。问题解决针对该问题,有两种方式可以解决,如下所示:添加启动参数--spring.config.additional-locationnohup/home/jdk1.8.0_251/bin/java-Xms256m-Xmx256m-j

See all articles