定时器Timer

1.Timer与TimerTask

有时候我们需要定时去完成某项任务,或者每隔一段时间去执行某个任务。这时候需要用到Timer类。

Timer定时器是一种工具,线程用其安排以后在后台线程中执行的任务。可安排任务执行一次,或者定期重复执行。 与每个Timer 对象相对应的是单个后台线程,用于顺序地执行所有计时器任务。计时器任务应该迅速完成。

Timer类的方法很少,使用起来比较方便。最常用的是schedule()方法。这个方法有四种重载形式:


schedule(TimerTask task, Date time)                                  安排在指定的时间执行指定的任务。
schedule(TimerTask task, long delay)                                  安排在指定延迟后执行指定的任务。
schedule(TimerTask task, Date firstTime, long period)     安排指定的任务在指定的时间开始进行重复的固定延迟执行。
schedule(TimerTask task, long delay, long period)            安排指定的任务从指定的延迟后开始进行重复的固定延迟执行。

 

TimerTask是什么呢?顾名思义,就是要完成的任务。

TimerTask是一个抽象类,实现了Runnable接口,所以它是一个线程类,使用TimerTask时要实现run()方法,即指定我们要完成的任务。

 

2.使用schedule()模拟一个定时炸弹

模拟一个定时炸弹,在程序启动10秒后爆炸。使用schedule(TimerTask task, long delay) 方法,delay代表延迟指定时间后执行任务。

 

public class TimerBomb {

	public static void main(String[] args) {

		//模拟一个定时炸弹,10秒后爆炸
		new Timer().schedule(new TimerTask() {
			
			@Override
			public void run() {
				System.out.println("爆炸了.......");
			}
		}, 10000);
		
		//每隔1秒输出一个时间
		while(true){
			try {
				Thread.sleep(1000);
				System.out.println(new Date().getSeconds());
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}


运行程序,程序在10秒后爆炸。

 

 

3.使用schedule()模拟一个连环定时炸弹

模拟一个定时炸弹,在程序启动5秒后爆炸,然后每隔3秒爆炸一次。使用schedule(TimerTask task, long delay, long period)方法,delay代表延迟指定时间后执行任务,period代表重复执行的任务间隔的时间段。

public class TimerBomb {

	public static void main(String[] args) {

		//模拟一个定时炸弹,5秒后爆炸,然后每隔3秒爆炸一次
		new Timer().schedule(new TimerTask() {
			
			@Override
			public void run() {
				System.out.println("爆炸了.......");
			}
		}, 5000, 3000);
		
		//每隔1秒输出一个时间
		while(true){
			try {
				System.out.println(new Date().getSeconds());
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}


运行程序

 

4.使用schedule()模拟一个时间间隔不同的连环定时炸弹

定义一个连环炸弹,2秒爆炸后间隔4秒爆炸,再过两秒爆炸后间隔4秒爆炸...

schedual()方法并没有提供这种情况的方法。所以要自己去是实现。

这就需要定义两个TimerTask,一个2秒炸,另一个4秒炸,并且一个要调用另一个,即每个TimerTask要创建一个新的TimerTask。

public class TimerBomb {

	//定义一个计数器,用于控制爆炸时间间隔
	private static int count = 0;
	
	public static void main(String[] args) {

		//模拟一个定时炸弹,分别间隔2秒和4秒爆炸
		new Timer().schedule(new MyTimerTask(), 2000+2000*count);
				
		//每隔1秒输出一个时间
		while(true){
			try {
				System.out.println(new Date().getSeconds());
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	//定义一个内部类继承TimerTask,因为每次爆炸后要新创建一个该类对象用于下一次爆炸,
	//所以不能再使用匿名内部类
	static class MyTimerTask extends TimerTask {

		@Override
		public void run() {
			System.out.println("爆炸了......");
			//count用于控制每次不同的爆炸时间间隔
			count = (count+1)%2;
			//每个炸弹爆炸完后,都要新建一个炸弹用于下次爆炸
			new Timer().schedule(new MyTimerTask(), 2000+2000*count);
		}
		
	}
	
}


运行程序

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux 定时器 timer_list 是一个内核数据结构,用于管理内核中的定时器。它是一个双向链表,每个节点表示一个定时器timer_list 的定义位于 `<linux/timer.h>` 头文件中。 每个 timer_list 节点的定义如下: ```c struct timer_list { struct list_head entry; // 定时器节点的链表指针 unsigned long expires; // 定时器的到期时间 void (*function)(unsigned long); // 定时器回调函数 unsigned long data; // 传递给回调函数的参数 struct tvec_base *base; // 定时器所属的时间轮 int slack; // 定时器的松弛时间 }; ``` 其中,`entry` 是一个 `list_head` 结构,用于将节点连接到定时器链表中。`expires` 表示定时器的到期时间,以 jiffies 单位表示。`function` 是定时器的回调函数,在定时器到期时被调用。`data` 是传递给回调函数的参数。`base` 表示定时器所属的时间轮,`slack` 是定时器的松弛时间,用于处理定时器的精度。 在使用 timer_list 时,可以使用以下函数进行初始化和操作: - `timer_setup(struct timer_list *timer, void (*function)(unsigned long), unsigned int flags)`:初始化一个定时器,并指定回调函数和标志。 - `init_timer(struct timer_list *timer)`:初始化一个定时器。 - `add_timer(struct timer_list *timer)`:将定时器添加到定时器链表中。 - `del_timer(struct timer_list *timer)`:从定时器链表中删除定时器。 - `mod_timer(struct timer_list *timer, unsigned long expires)`:修改定时器的到期时间。 这些函数可以通过 `<linux/timer.h>` 头文件中的宏来调用。通过操作 timer_list,可以实现在 Linux 内核中的定时器功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值