承上一篇timer实现文章,写了几个小的验证测试程序,主要测试以下几个问题
(1) Timer.cancel()能不能立即终止正在执行的timer。
(2) Thread.stop()能不能立即终止正在执行的thread。
(3) 同一个timer调度多个TimerTask时会不会相互影响。
实例代码及结果分析
(1) timer.cancel()能不能立即终止正在执行的timer? 不能。
代码如下:
import java.util.Timer;
import java.util.TimerTask;
public class test_timer {
/**
* @param args
*/
static int count=0;
public static void main(String[] args) {
// TODO Auto-generated method stub
Timer tm=new Timer();
tm.schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(count);
count++;
}
}, 0, 5000);
try {
Thread.sleep(2000);
tm.cancel();
System.out.println("canceled");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
运行结果如下:
canceled
0
(2) Thread.stop()能不能立即终止正在执行的thread? 能。
代码如下:
public class test_thread {
public static void main(String[] argv){
Thread t=new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("hello ");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("world");
}
});
t.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("stop!");
t.stop();
}
}
hello
stop!
运行结果分析:输出hello后thread进入sleep阻塞,然后主线程中执行了stop方法然后打印stop!,之后就没有输出sleep阻塞后应该输出的world,说明stop方法可以立即终止执行中的thread线程,但这个方法同时也可能导致死锁,所以被jdk弃用了。
(3) 同一个timer调度多个TimerTask时会不会相互影响?会。
代码如下:
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class test_one_timer_multi_task {
class TimertaskA extends TimerTask{
@Override
public void run() {
// TODO Auto-generated method stub
Calendar c=Calendar.getInstance();
Date d=c.getTime();
System.out.println("this is A :"+d.getMinutes()+":"+d.getSeconds());
}
}
class TimertaskB extends TimerTask{
@Override
public void run() {
// TODO Auto-generated method stub
Calendar c=Calendar.getInstance();
Date d=c.getTime();
System.out.println("this is B :"+d.getMinutes()+":"+d.getSeconds());
try {
Thread.sleep(8000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
c=Calendar.getInstance();
d=c.getTime();
System.out.println("this is B after sleep:"+d.getMinutes()+":"+d.getSeconds());
}
}
public static void main(String[] args){
Timer ti=new Timer();
test_one_timer_multi_task totmt=new test_one_timer_multi_task();
ti.scheduleAtFixedRate(totmt.new TimertaskA(), 0, 3000);
ti.scheduleAtFixedRate(totmt.new TimertaskB(), 0, 5000);
}
}
运行结果如下:
this is A :5:29
this is B :5:29
this is B after sleep:5:37
this is A :5:37
this is B :5:37
this is B after sleep:5:45
this is A :5:45
this is A :5:45
this is B :5:45
运行结果分析:TimerTaskA每3s执行一次,TimerTaskB每5s执行一次还要在执行过程中休眠8s,从结果来看,如果同一timer调度的多个任务相互没有影响的话A应该会在5:32打印一次,但实际是被B阻塞到5:37 B完成休眠以后才被调度,所以印证了jdk文档中的描述:与每个 Timer 对象相对应的是单个后台线程,用于顺序地执行所有计时器任务。计时器任务应该迅速完成。如果完成某个计时器任务的时间太长,那么它会“独占”计时器的任务执行线程。因此,这就可能延迟后续任务的执行,而这些任务就可能“堆在一起”,并且在上述不友好的任务最终完成时才能够被快速连续地执行。