timer schudele

本文详细介绍了 Java 中 Timer 类的功能及使用方法,包括不同构造器的使用场景、关键方法如 schedule 和 cancel 的工作原理,以及如何通过 TimerTask 实现具体的定时任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

timer schudele的使用

本次使用主要围绕着以下即方面开展:

  1. java api介绍
  2. schedule在任务调度上的使用
  3. 任务的开启和取消
  4. 任务执行过程中的异常情况

java API的介绍

timer 构造器
Timer() Creates a new timer.
Timer(boolean isDaemon) Creates a new timer whose associated thread may be specified to run as a daemon.
Timer(String name) Creates a new timer whose associated thread has the specified name.
Timer(String name, boolean isDaemon) Creates a new timer whose associated thread has the specified name, and may be specified to run as a daemon.

timer 的方法
Modifier and TypeMethod and Description
voidcancel() Terminates this timer, discarding any currently scheduled tasks.
intpurge() Removes all cancelled tasks from this timer’s task queue.
voidschedule(TimerTask task, Date time) Schedules the specified task for execution at the specified time.
voidschedule(TimerTask task, Date firstTime, long period) Schedules the specified task for repeated fixed-delay execution, beginning at the specified time.
voidschedule(TimerTask task, long delay) Schedules the specified task for execution after the specified delay.
voidschedule(TimerTask task, long delay, long period) Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay.
voidscheduleAtFixedRate(TimerTask task, Date firstTime, long period) Schedules the specified task for repeated fixed-rate execution, beginning at the specified time.
voidscheduleAtFixedRate(TimerTask task, long delay, long period) Schedules the specified task for repeated fixed-rate execution, beginning after the specified delay.

除了以上方法以外,还继承了对象的一些方法,如下

继承对象的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

以上,这些方法中,主要要明白,该类具有什么样的功能,什么时候使用该类。任务管理方法的使用,同时该方法定义的任务都是在一个独立的线程中运行。
下面再介绍与该任务管理器有关的timer Task类。
如果你要定义一个定时任务,只需要将你的任务类继承timerTask类就行 了,执行的任务主要在run()方法进行实现。timerTask类继承了runnable类,因此是线程安全的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

coder_jt

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值