interrupt方法使用详解,如何使用interrupt方法?

一.interrupt方法

结束线程在调用Object类的wait方法或该类的join方法、sleep方法过程中的阻塞状态,并在调用wait、join和sleep方法处产生InterruptedException异常

1.例1

import java.util.Date;

public class Test {

	public static void main(String[] args) {
		TimeThread timeThread = new TimeThread();//创建了一个线程
		timeThread.start();//调用start()方法,使timeThread线程就绪
		try {
			Thread.sleep(10000);//为了看出interrupt()方法的效果,停顿10s
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		timeThread.interrupt();//中断timeThread线程,在timeThread线程阻塞处爆发异常并处理该异常,处理完后程序向下执行,打印输出date
	}
}

class TimeThread extends Thread{

	@Override
	public void run() {
		while (true) {
			Date date = new Date();
			try {
				sleep(60000);//能直接使用sleep()方法,因为TimeThread继承自Thread类,继承了该类的方法
				System.out.println(date);//不执行,因为调用interrupt方法爆发异常后,直接由e.printStackTrace();处理异常,所以try里的语句不再执行
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(date);//处理完异常后,打印输出该句话
		}
	}
}

执行后结果在这里插入图片描述

2.例2

import java.text.SimpleDateFormat;
import java.util.Date;

class TimeThread extends Thread {

	public void run() {
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss:sss");
		String beforeTime = sdf.format(new Date());
		System.out.println("beforeTime:"+beforeTime);//打印输出beforeTime:时间
		try {
			sleep(30000);// 30秒后执行后面代码,timeThread线程阻塞
			//timeThread.interrupt()该方法调用后,中断阻塞,该处爆发异常
		} catch (Exception e) {
			System.out.println("程序捕获了InterruptedException异常!");//处理异常,打印输出
		}
		String afterTime = sdf.format(new Date());
		System.out.println("afterTime:"+afterTime);//打印afterTime:时间
	}
}

public class Program {
	public static void main(String[] args) {//主线程
		TimeThread timeThread = new TimeThread();//创建timeThread线程
		timeThread.start();//timeThread线程就绪
		try{
			Thread.sleep(1000);//主线程阻塞1s
		}catch(InterruptedException e){
			e.printStackTrace();
		}
		timeThread.interrupt();
	}
}

打印结果
在这里插入图片描述
理论上beforeTime和afterTime应该相差30s,但是因为该线程类执行1s后调用了interrupt方法使得该线程提前中断了阻塞状态,导致beforeTime和afterTime不相差30s

3.例3

class CounterThread extends Thread {

	Object lockObj;//定义一个全局变量

	public CounterThread(Object lockObj) {
		this.lockObj = lockObj;//给全局变量赋值
	}

	@Override
	public void run() {
		synchronized (lockObj) {
			System.out.println("计数器线程正在执行......");
			try {
				lockObj.wait();//当线程执行该行代码后,线程进入阻塞状态;但由于10秒后主线程执行了“counterThread.interrupt();”代码使得该线程阻塞状态结束
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

public class Test {

	public static void main(String[] args) {
		Object lockObj = new Object();//创建lockObj对象
		CounterThread counterThread = new CounterThread(lockObj);//创建counterThread线程,调用CounterThread的构造方法,将lockObj传进去
		counterThread.start();//counterThread调用start方法,就绪
		try {
			Thread.sleep(10000);//停顿10s
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		counterThread.interrupt();//结束counterThread线程的阻塞状态
	}
}

在这里插入图片描述

4.例4

import java.util.Date;

class TimeThread extends Thread{
	
	@Override
	public void run() {//计数器线程进入阻塞状态后,时间线程获得了CPU的使用权,进入运行状态
		for(int i=0;i<=2; i++){
			System.out.println("时间线程:"+new Date());
			try {
				Thread.sleep(10000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}//时间线程执行完至少需要30s
		}
	}
}

class CounterThread extends Thread {
	
	private TimeThread timeThread;
	
	public CounterThread(TimeThread timeThread){
		this.timeThread = timeThread;
	}
	//计数器线程抢占到CPU的资源,优先进入运行状态
	@Override
	public void run() {
		for(int i=1;i<=3; i++){
			if(i==2){
				try {
					timeThread.join();//计数器线程进入阻塞状态,时间线程进入就绪状态
				} catch (InterruptedException e) {
					System.out.println("计数器线程提前结束阻塞状态");
				}
			}
			System.out.println("计数器线程:"+i);
		}
	}
}

public class Program {
	public static void main(String[] args) {//主线程
		TimeThread timeThread = new TimeThread();//创建timeThread线程
		timeThread.start();//timeThread线程就绪
		CounterThread counterThread = new CounterThread(timeThread);//创建counterThread线程
		counterThread.start();//counterThread线程就绪
		try {
			Thread.sleep(15000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		counterThread.interrupt();//时间线程至少需要消耗30秒才能结束,而15秒后计数器线程调用了interrupt方法致使该计数器线程提前结束阻塞状态
	}
}

执行结果
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值