Timer定时器详解

Timer定时器主要做定时任务或者按照一定的时间间隔做任务,例如每天4点钟定时执行作业等

Timer的特性
1、它属于单线程的,每创建个Timer实例,就会创建一个新线程
2、Timer默认情况下不是守护线程,可以设置为守护线程new Timer(true),守护线程再进程中没有其他线程时,守护线程将销毁
3、TimerTask是以队列的方式一个个的执行
4、TimerTask中的cancel()方法将自身中从队列中清除
5、Timer中的cancel()方法是将任务队列中的全部任务进行取消,有时并不一定停止任务,原因是Timer类中的cancel()方法有时并没有竞争到锁,
(这个可以从Timer源码中看到,TimerThread和TaskQueue两个内部类)
6、TimerTask中的run方法无法抛出,所以要进行try catch捕获,如果其中任何一个任务发生异常没有被捕获,则其他任务也将被终止

方法详解:
(1)schedule(TimerTask task, Date executeTime)
当executeTime<=currentTime时,task任务会在currentTimer立即执行
当executeTime>currentTime时,task会在未来的executeTime执行

(2)schedule(TimerTask task, Date firstTime, long period)
当firstTime <=currentTime时,task任务会在currentTimer立即执行,
当firstTime >currentTime时,task会在未来的executeTime执行,
执行任务所用的时间taskUsedTime<peroid,则下一个任务执行的时间是上次任务执行完成的时间+peroid,任务按时间间隔peroid周期性执行任务
执行任务所用的时间taskUsedTime>peroid,则下一个任务执行的时间是上次任务执行完成的时间+taskUsedTime,任务按时间间隔taskUsedTime 周期性执行任务


(3)schedule(TimerTask task, long delay)
任务延迟delay毫秒进行执行

(4)schedule(TimerTask task, long delay, long period)
A、延迟delay毫秒第一次执行,
B、执行任务所用的时间taskUsedTime<peroid,则下一个任务执行的时间是上次任务执行完成的时间+peroid, 任务按时间间隔peroid周期性执行任务
C、执行任务所用的时间taskUsedTime>peroid,则下一个任务执行的时间是上次任务执行完成的时间+taskUsedTime, 任务按时间间隔taskUsedTime 周期性执行任务

(5)scheduleAtFixedRate(TimerTask task, long delay, long period)

(6)scheduleAtFixedRate(TimerTask task, Date firstTime,  long period)
         startTime = currentTime
A、当firstTime>currentTime,任务则在currentTime执行
B、当firstTime<currentTime,任务会发生追赶执行,追赶执行的次数expectCount=(currentTime-firstTime)/peroid+1;
  第一个peroid属于追赶阶段,如果追赶上则等待执行startTime+peroid时间任务,如果没有追赶上则直接执行startTime+peroid时间的任务




  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C#中,有多种方式可以使用Timer类来实现定时器功能。其中包括System.Threading.Timer、System.Windows.Forms.Timer和System.Timers.Timer。\[1\] 下面是一个使用System.Windows.Forms.Timer的示例代码: ```csharp using System; using System.Windows.Forms; namespace Timer1 { public partial class Form1 : Form { public int systemCounter = 0; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // 设置Timer1控件可用 this.timer1.Enabled = false; // 设置Timer1时间间隔 this.timer1.Interval = 1000; } private void btnStart_Click(object sender, EventArgs e) { this.timer1.Start(); } private void btnStop_Click(object sender, EventArgs e) { this.timer1.Stop(); } private void timer1_Tick(object sender, EventArgs e) { systemCounter += 1; this.textBox1.Text = systemCounter.ToString().Trim(); } } } ``` 在这个示例中,我们使用了System.Windows.Forms.Timer控件来实现定时器功能。在Form1_Load方法中,我们设置了Timer1的Enabled属性为false,表示初始状态下定时器是停止的,然后设置了时间间隔为1秒。在btnStart_Click方法中,我们调用timer1.Start()方法来启动定时器。在btnStop_Click方法中,我们调用timer1.Stop()方法来停止定时器。在timer1_Tick方法中,我们每次定时器触发时,将systemCounter加1,并将其显示在textBox1中。 这只是一个使用System.Windows.Forms.Timer的示例,你也可以根据需要选择其他Timer类来实现定时器功能。 #### 引用[.reference_title] - *1* *2* *3* [C#三种定时器Timer详解](https://blog.csdn.net/qq_57798018/article/details/128243618)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值