在JDK1.5之前,有个定时器的类叫Timer,下面代码简单介绍Timer类的使用
package threadTest;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
/**
* 传统定时器的创建和应用
* @author huochaigun
* 2014年12月9日 上午11:23:22
*/
public class TraditionalTimerTest {
public static void main(String[] args) {
//传统定时器的创建 2秒后就运行一次
new Timer().schedule(new TimerTask() {
@Override
public void run() {
System.out.println("bomobing...");
}
}, 2000);
while(true){
System.out.println(new Date().getSeconds());
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
程序运行结果如下:
45
46
bomobing...
47
48
49
50
51
52
上面的例子,介绍了简单的用法,现在我们要求炸弹,第一次2秒后爆炸,之后每隔3秒后都爆炸一次,那么代码做如下修改
package threadTest;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
/**
* 传统定时器的创建和应用
*
* @author huochaigun 2014年12月9日 上午11:23:22
*/
public class TraditionalTimerTest {
public static void main(String[] args) {
// 传统定时器的创应用 第1次2秒后运行 之后每隔3秒运行一次
new Timer().schedule(new TimerTask() {
@Override
public void run() {
System.out.println("bomobing...");
}
}, 2000, 3000);
while (true) {
System.out.println(new Date().getSeconds());
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
程序运行结果如下:
21
22
bomobing...
23
24
25
bomobing...
26
27
28
bomobing...
29
30
再往深层次发展,现在我们的要求又变了,要求炸弹第1次爆炸为2秒,然后4秒在爆炸,然后2秒后再爆炸,4秒后再炸,依次类推?
那么我们应该怎么整理代码呢,【递归 调用自己(那么这里TimerTask就必须得单独抽取出来做个一个单独的类使用了) 判断技术和偶数 做修改时间】
代码如下:
package org.yla;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
/**
* 传统定时任务的创建
*
* @author yangluan 2014年12月7日下午1:24:08
*/
public class TraditionalTimerTaskTest {
/**
* 全局变量 控制num 不是0 就是1
*/
private static int num = 0;
public static void main(String[] args) {
// 声明一个自己的定义的MyTimerTask的炸弹类
class MyTimerTask extends TimerTask {
@Override
public void run() {
num =(num+1)%2;
System.out.println("bomling...");
// 炸弹里面再生成新的炸弹
new Timer().schedule(new MyTimerTask(), 2000+2000*num);
}
}
new Timer().schedule(new MyTimerTask(), 2000);
while (true) {
System.out.println(new Date().getSeconds());
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
运行结果:
21
22
bomling...
23
24
25
26
bomling...
27
28
bomling...
29
30
31
32
bomling...
33
34
上面的例子再使用例外一种写法:
代码如下:
TraditionalTimerTaskTest类:
package org.yla;
import java.util.Date;
import java.util.Timer;
import java.util.concurrent.TimeUnit;
/**
* 传统定时任务的创建
*
* @author huo_chai_gun 2014年12月7日下午1:24:08
*/
public class TraditionalTimerTaskTest {
public static int count=0;
public static void main(String[] args) {
new Timer().schedule(new MyTimerTask(), 2000);
while (true) {
System.out.println(new Date().getSeconds());
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
MyTimerTask类:
package org.yla;
import java.util.Timer;
import java.util.TimerTask;
/**
* @author huo_chai_gun
* 2014年12月9日下午10:48:09
*/
public class MyTimerTask extends TimerTask {
//接收TraditionalTimerTaskTest中定义的count static全局变量的使用法则
static int count =TraditionalTimerTaskTest.count;
@Override
public void run() {
System.out.println("count: "+count);
count=(count+1)%2;
System.out.println("爆炸...");
new Timer().schedule(new MyTimerTask(), 2000+2000*count);
}
}
运行结果:
43
44
count: 0
爆炸...
45
46
47
48
count: 1
爆炸...
49
50
count: 0
爆炸...
51
52
53
54
count: 1
爆炸...
55