Timer定时器使用

 一个程序

需要一个程序2秒运行一次,之后又4秒允许一次,如此循环。

写道

/**
* 需要一个程序2秒允许一次4秒允许一次如此循环
* @author Administrator
*
*/
public class TraditionnalTimer2 {

private static int count;

public static void main(String[] args) {
class MyTimerTask extends TimerTask{

@Override
public void run() {
//每运行一次,0和1不停的交替
count=(count+1)%2;
System.out.println("boding !!");
new Timer().schedule(new MyTimerTask(), 2000+2000*count);
}

}

new Timer().schedule(new MyTimerTask(), 2000);

while(true) {
try {
System.out.println(new Date().getSeconds());
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

猜你喜欢

转载自hualom.iteye.com/blog/1513965