java5编程(2)传统定时器技术回顾,张孝祥老师经典讲解总结 (三)

2014-11-24 07:45:58 · 作者: · 浏览: 3
(), 2000);
}
}


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

while(true){
System.out.println(new Date().getSeconds());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

}


5,每隔不同的时间,来进行循环炸,2秒炸一次,四秒再炸一次,如此循环。

别炸啦,快哭了。

[java]
package com.itm.thread;


import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class TraditionalTimerChangeTest {

private static int count = 0;

public static void main(String[] args) {

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();
}
}
}

}
package com.itm.thread;


import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class TraditionalTimerChangeTest {

private static int count = 0;

public static void main(String[] args) {

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();
}
}
}

}


作者 itm_hadf