在沉睡中停止线程

如果在sleep状态下停止某一线程,会进入catch语句,并且清除停止状态值,使之变成false


class MyThread extends Thread{
	@Override
	public void run(){
		super.run();
		try{
			System.out.println("run begin");
			Thread.sleep(200000);
			System.out.println("run end");
		}catch(InterruptedException e){
			System.out.println("在沉睡中被停止,进入catch! ---" +this.isInterrupted());
			e.printStackTrace();
		}
	}
}


public class Run_sleep_interrupt{
	public static void main(String [] args){
		try{
			MyThread thread = new MyThread();
			thread.start();
			thread.sleep(200);
			thread.interrupt();
		}catch(InterruptedException e){
			System.out.println("main catch");
			e.printStackTrace();
		}

		System.out.println("end!");
	}
}
//运行结果: 
//  run begin
//  end!
//  在沉睡中被停止,进入catch! ---false
//  java.lang.InterruptedExcption: sleep interrupted at java.lang.Thread.sleep interrupted
//  at Mythread.run(Run_sleep_interrupt.java:7)

//如果在sleep状态下停止某一线程,会进入catch语句,并且清除停止状态值,使之变成false




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值