黑马程序员技术交流社区
标题:
timer类的交替调度
[打印本页]
作者:
xgm
时间:
2016-3-18 00:31
标题:
timer类的交替调度
* 目的:进一步了解Timer类,完成Timer的交替调度
*
*/
public class TestTimer2 {
static Timer timer = new Timer();
public static void main(String[] args) {
timer.schedule(new MyTimerTask(), 2000);
/*timer.schedule(new TimerTask(){
public void run() {
System.out.println("缘分这东西太难搞了");
new Timer().schedule(this, 4000); //Task already scheduled or cancelled
}}, 2000);*/
int i = 0;
while(true){
System.out.println(++i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
static int num = 0;
static class MyTimerTask extends TimerTask{
public void run() {
num = (num+1)%2; //判断余数为1或者0
System.out.println("头发花白的程序员");
timer.schedule(new MyTimerTask(), 2000*(num+1)); //实现交替进行
}
}
}
复制代码
作者:
zx7660
时间:
2016-3-18 12:49
恩 不错
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2