1.java代码
package com.xxxx.util;
import java.util.Timer;
import java.util.TimerTask;
public class Test {
static int k = 1;
private static int delay = 1000;
private static int cacheTime = 1000;
private static int cacheTime1 = 8000;
static Timer timer2 = new Timer();
public static void main(String[] args) {
t1();
t2();
}
public static void t1(){
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("打印");
}
}, delay, cacheTime);//延迟delay毫秒后,执行第一次任务,每隔cacheTime毫秒后执行任务(设置每1秒执行一次)
}
public static void t2(){
timer2.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("测试");
k = k + 1;
if(k==5){
timer2.cancel();
}
}
}, delay, cacheTime1);//延迟delay毫秒后,执行第一次任务,每隔cacheTime1毫秒后执行任务(设置每8秒执行一次)
}
}