package cn.itcast.Thread;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class TraditionalTimerTest {
private static int count = 0;
public static void main(String[] args) {
/*new Timer().schedule(new TimerTask() {
@Override
public void run() {
System.out.println("bombing!..");
}
}, 10000,3000);
while(true){
System.out.println("时间:"+new Date().getSeconds());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}*/
//2秒和4秒交替执行一次
class MyTimerTask extends TimerTask{
@Override
public void run() {
count = (count+1)%2;
System.out.println("bombing!..");
new Timer().schedule(
new MyTimerTask(),2000+2000*count);
}
}
new Timer().schedule(new MyTimerTask(), 2000);
while(true){
System.out.println("时间:"+new Date().getSeconds());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
传统的定时器技术回顾

最新推荐文章于 2020-06-16 11:04:01 发布