线程停止 interrupt,interrupted和isInterrupted

停止线程在java语言中不并不像break语句哪样干脆,不是说停止就停止。
在java中有3种停止线程的方式:
1.线程正常退出,也就是当run方法正常完成后终止线程。
2.使用stop方法强制终止线程,但是不推荐使用这个方法,它是一个作废过期的方法,使用它们会产生不可预料的后果。
3.使用interrupt方法中断线程。

但是使用interrupt()方法的使用并不像for +break那样马上就停止循环
例子:

public class MyThread implements Runnable {

	@Override
	public void run() {
		// TODO Auto-generated method stub
		for(int i=0;i<1000;i++) {
			System.out.println("i:"+i +" 当前线程:"+Thread.currentThread().getName());
		}
	}
	
	public static void main(String[] args) throws InterruptedException {
		MyThread myThread = new MyThread();
		Thread thread = new Thread(myThread);
		thread.start();
	    thread.interrupt();
	
	}

}

在这里插入图片描述
可以看出他依然打印到了999没有停止该线程

要停止线程,先要知道如何判断线程状态是不是停止的

1.interrupted
public static boolean interrupted()
测试当前线程是否已经中断

2.isInterrupted
public boolean isInterrupted()
测试线程是否已经中断

看看例子
1.interrupted

public class MyThread implements Runnable {

	@Override
	public void run() {
		// TODO Auto-generated method stub
		for(int i=0;i<1000;i++) {
			System.out.println("i:"+i );
		}
	}
	
	public static void main(String[] args) throws InterruptedException {
		MyThread myThread = new MyThread();
		Thread thread = new Thread(myThread,"Mythread");
		thread.start();
	    thread.interrupt();   
	    System.out.println("线程是否停止1="+Thread.interrupted()+" 当前线程:"+Thread.currentThread().getName());
	    System.out.println("线程是否停止2="+Thread.interrupted()+" 当前线程:"+Thread.currentThread().getName());
	}

}

在这里插入图片描述
因为interrupted判断的是当前线程 而当前线程是main线程,而interrupt是中断当前线程也就是中断main线程,那如何中断main线程呢

public static void main(String[] args){
Thread.currentThread().interrupt(); //Thread.currentThread()获取当前线程信息
System.out.println(“是否停止1:”+Thread.interrupted());
System.out.println(“是否停止2:”+Thread.interrupted());
System.out.println(“end”);
}
在这里插入图片描述
而为什么第二次又位false了呢,它的官方文档中解释

测试当前线程是否已经中断。线程的中断状态由该方法清除,换句话说,如果连续调用该方法,则第二次调用将返回false,所以interrupted方法具有清除状态的功能
在这里插入图片描述

如果没有中断main线程的话,两次调用都会返回false。清除这种状态是相对的,意思是如果线程是中断的(true),那么第二次调用该方法会清除这种状态,返回false,如果线程没有中断,那么第二次依然不会改变这种状态

2.isInterrupted
此方法不是静态
1.

public class MyThread implements Runnable {
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		for(int i=0;i<1999;i++) {
			System.out.println(i+1);
		}
	}
	
	public static void main(String[] args) throws InterruptedException  {
		MyThread myThread = new MyThread();
		Thread thread = new Thread(myThread,"Mythread");
		thread.start();
	    thread.interrupt();
	    System.out.println("线程是否停止1="+thread.isInterrupted());
	    System.out.println("线程是否停止2="+thread.isInterrupted());
	}

在这里插入图片描述
可以看到我將Mythread线程interrupt停止了,两者返回的都是true,并且不会清除这种状态。

public static void main(String[] args) throws InterruptedException  {
		MyThread myThread = new MyThread();
		Thread thread = new Thread(myThread,"Mythread");
		thread.start();
	    thread.interrupt();
	    System.out.println("线程是否停止1="+Thread.currentThread().isInterrupted());
	    System.out.println("线程是否停止2="+Thread.currentThread().isInterrupted());
	}

在这里插入图片描述
输出语句中我是判断的main线程是否停止我并没有停止main线程所以返回的是false

public static void main(String[] args) throws InterruptedException  {
		MyThread myThread = new MyThread();
		Thread thread = new Thread(myThread,"Mythread");
		thread.start();
	    Thread.currentThread().interrupt();
	    System.out.println("线程是否停止1="+Thread.currentThread().isInterrupted());
	    System.out.println("线程是否停止2="+Thread.currentThread().isInterrupted());
	}

在这里插入图片描述
Thread.currentThread().interrupt();可以看到我将main线程停止了,所以输出语句返回的true

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值