155.高级主题之定时调度

本文介绍了Java中使用Timer和TimerTask实现定时任务调度的方法。通过创建Timer实例,可以设置不同的参数来安排任务的执行,如延迟执行、周期性执行等。同时,TimerTask作为任务类,需要继承并实现其抽象方法run()来定义任务内容。示例代码展示了如何在不同场景下安排任务执行。
摘要由CSDN通过智能技术生成

任务定时调度在多线程中有重要的地位和作用

通过Timer和Timetask,我们可以实现定时启动某个线程
java.util.Timer

在这种实现方式中,Timer类作用是类似闹钟的功能,也就是定时或者每隔一定时间触发一次线程。其实,Timer类本身实现的就是一个线程,只是这个线程是用来实现调用其它线程的。

constructor
ConstructorDescription
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.
Method
Method Modifier and TypeDescription
void cancel()Terminates this timer, discarding any currently scheduled tasks.
int purge()Removes all cancelled tasks from this timer’s task queue.
void schedule 1(TimerTask task, long delay)Schedules the specified task for execution after the specified delay.
void schedule(TimerTask task, long delay, long period)Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay.
void schedule(TimerTask task, Date time)Schedules the specified task for execution at the specified time.
void schedule(TimerTask task, Date firstTime, long period)Schedules the specified task for repeated fixed-delay execution, beginning at the specified time.
void scheduleAtFixedRate(TimerTask task, long delay, long period)Schedules the specified task for repeated fixed-rate execution, beginning after the specified delay.
void scheduleAtFixedRate(TimerTask task, Date firstTime, long period)Schedules the specified task for repeated fixed-rate execution, beginning at the specified time.
java.util.TimerTask

TimerTask类是一个抽象类,该类实现了Runnable接口,所以该类具备多线程的能力。在这种实现方式中,通过继承TimerTask使该类获得多线程的能力,将需要多线程执行的代码书写在run方法内部,然后通过Timer类启动线程的执行。

constructor
ConstructorDescription
protected TimerTask()Creates a new timer task.
Method
Method Modifier and TypeDescription
boolean cancel()Cancels this timer task.
abstract void run 2()The action to be performed by this timer task.
long scheduledExecutionTime()Returns the scheduled execution time of the most recent actual execution of this task.
进行代码练习
package zy.thread.advancedTopics;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Timer;
import java.util.TimerTask;

/*
 * 任务调度: Timer 和 TimerTask
 */
public class TimerTest01 {
	public static void main(String[] args) {
		Timer t = new Timer();
		//执行安排
		//void	schedule [1](TimerTask task, long delay)
		t.schedule(new myTask(), 5000); //等待5000毫秒后执行一次
		
		//void	schedule(TimerTask task, Date firstTime, long period)
		t.schedule(new myTask(), new Date(500L), 100);
		
		//void	schedule(TimerTask task, long delay, long period)
		t.schedule(new myTask(), 500, 100); //等待500毫秒后执行一次,然后每隔100毫秒执行一次
		
		Calendar cal = new GregorianCalendar(2020, 10, 5, 1, 12, 54);
		t.schedule(new myTask(), cal.getTime(), 100);
	}
}

//任务类、工作类
class myTask extends TimerTask{
	public void run() {
		for(int i=0; i<6; ++i)
			System.out.println("----------冥想----------");
		System.out.println("_____end_____");
	}
}

  1. schedule用于指定任务及其他属性 ↩︎

  2. 抽象方法需要实现 ↩︎

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值