传统定时器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package cn.itcast.heima2;

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() {//todo 第一次炸是10s后,以后每隔3s炸一次

@Override
public void run() {
System.out.println("bombing!");

}
}, 10000,3000);*/


class MyTimerTask extends TimerTask{

@Override
public void run() {
count = (count+1)%2;
System.out.println("bombing!");
new Timer().schedule(/*new TimerTask() {

@Override
public void run() {
System.out.println("bombing!");
}
}*/new MyTimerTask(),2000+2000*count);
}
}

new Timer().schedule(new MyTimerTask(), 2000);//间隔2s和4s炸

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

}