Java Timer

Timer位于java.util.Timer中,可在帮助文档中查看其详细信息




schedule(TimerTask task, Date time)
Schedules the specified task for execution at the specified time. 调度指定任务在指定时间执行


schedule(TimerTask task, Date firstTime, long period)
Schedules the specified task for repeated fixed-delay execution, beginning at the specified time. 调度指定任务自firstTime后每隔period时间自动运行


schedule(TimerTask task, long delay)
Schedules the specified task for execution after the specified delay. 调度指定任务延迟delay时间后开始执行


schedule(TimerTask task, long delay, long period)
Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay. 调度指定任务延迟delay时间后开始执行并每隔period时间执行一次


scheduleAtFixedRate(TimerTask task, Date firstTime, long period)
Schedules the specified task for repeated fixed-rate execution, beginning at the specified time.


scheduleAtFixedRate(TimerTask task, long delay, long period)
Schedules the specified task for repeated fixed-rate execution, beginning after the specified delay.
Java实时性存在误差,(约定1s执行有可能1.5秒才执行)此函数指明以固定的频率执行(若此次执行延误了0.5秒会在下次尽量提前0.5秒,保持频率基本不变)

程序1:

package test;

import java.io.IOException;
import java.util.Timer;

public class TimerTest {
	public static void main(String[] args) {
		Timer timer = new Timer();
		timer.schedule(new MyTask(), 1000, 2000);
        // 在1秒后执行此任务,每次间隔2秒,如果传递一个Data参数,就可以在某个固定的时间执行这个任务.
        time.schedule(new MyTask2(), 2000, 3000);
		while (true) {
			try {
				int ch = System.in.read();
				if (ch - 'c' == 0) {
					timer.cancel();// 使用这个方法退出任务
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	static class MyTask extends java.util.TimerTask {
		@Override
            public void run() {
			System.out.println("________");
		}
	}
    static class MyTask2 extends java.util.TimerTask{
        @Override
            public void run(){
            System.out.println("+++++++");
        }
    }
}

程序2:

package test;

import java.io.IOException;
import java.util.Timer;

/**
 * 本类给出了使用Timer和TimerTaske的主要方法,其中包括定制任务,添加任务
 * 退出任务,退出定时器.
 * 因为TimerTask的status域是包级可访问的,所以没有办法在java.util.包外
 * 得到其状态,这对编程造成一些不便 .我们不能判断某个Task的状态了.
 * 
 */

public class TimerTestComplex {

	public static void main(String[] args) {
		Timer timer = new Timer();
		MyTask myTask1 = new MyTask();
		MyTask myTask2 = new MyTask();
		myTask2.setInfo("myTask-2");
		timer.schedule(myTask1, 1000, 2000);
		timer.scheduleAtFixedRate(myTask2, 2000, 3000);
		while (true) {
			try {
				byte[] info = new byte[1024];
				int len = System.in.read(info);
				String strInfo = new String(info, 0, len, "GBK");// 从控制台读出信息
				if (strInfo.charAt(strInfo.length() - 1) == ' ') {
					strInfo = strInfo.substring(0, strInfo.length() - 2);
				}
				if (strInfo.startsWith("Cancel-1")) {
					myTask1.cancel();// 退出单个任务
					// 其实应该在这里判断myTask2是否也退出了,是的话就应该break.但是因为无法在包外得到
					// myTask2的状态,所以,这里不能做出是否退出循环的判断.
				} else if (strInfo.startsWith("Cancel-2")) {
					myTask2.cancel();
				} else if (strInfo.startsWith("Cancel-All")) {
					timer.cancel();// 退出Timer
					break;
				} else {
					// 只对myTask1作出判断,偷个懒^_^
					myTask1.setInfo(strInfo);
				}
			} catch (IOException e) { // TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	static class MyTask extends java.util.TimerTask {
		String info = "^_^";
		@Override
		public void run() {
			System.out.println(info);
		}
		public String getInfo() {
			return info;
		}
		public void setInfo(String info) {
			this.info = info;
		}
	}

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值