java线程控制——timer与thread小测试+timer.cancel()与thread.stop()实例

承上一篇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


结果分析:先输出了canceled再输出0 说明timer调度timertask以后TimerTask阻塞在sleep(3000)这里的时候timer被cancel了,但是依旧输出了0,说明cancel之后,在cancel之前被调度的TimerTask仍旧会继续执行完(线程安全角度防止了死锁),但cancel之后就不会再有任务被调度了。

(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 对象相对应的是单个后台线程,用于顺序地执行所有计时器任务。计时器任务应该迅速完成。如果完成某个计时器任务的时间太长,那么它会“独占”计时器的任务执行线程。因此,这就可能延迟后续任务的执行,而这些任务就可能“堆在一起”,并且在上述不友好的任务最终完成时才能够被快速连续地执行。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值