1.需求:每隔两移输出一次pop
一. public static void main(String[] args){
class myTimerTask extends TimerTask{
new Timer.schedule(new myTimerTask (){
Run(){
System.out.println("pop!");
}
},2000)
}
new Timer().schedule(new myTimerTask (),2000);
while(true){
System.out.println(new Date().getSeconds());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
2.需求:两移与四移间隔输出
二.
public class traditionalTimerTest {
private static int count = 0;
public static void main(String[] args) {
class myTimerTask extends TimerTask{
public void run() {
count =(count+1)%2;
System.out.println("bpping");
new Timer().schedule( new myTimerTask() /*new TimerTask(){
public void run() {
System.out.println("apping");
}
}*/, 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();
}
}
}
}