使用异常法来停止线程(java)

大家都知道,java里面有3种方法来停止线程:

1.run()方法执行完毕,自动退出。

2.使用stop方法来强行退出,但是不推荐使用这个方法,因为此方法已过期。

3.使用interrupt方法中断线程。

注意,我们推荐使用interrupt()方法来停止线程,但是单纯调用interrupt()方法仅仅是在当前线程中打了一个停止的标记,并没有真的停止线程。

下面来看一个例子:

 

MyThread类:
public class MyThread extends Thread {
    public void run(){
        super.run();
            for(int i=0;i<500000;i++)
            {
                if (this.interrupted()) {
                    System.out.println("线程已经是停止状态了,我要退出了");
                    break;
                }
                System.out.println("i="+(i+1));
            }
            System.out.println("我在for的下面,如果我输出,那么就证明线程没有被停止!");
         }
    }
test类:
public class test {
        public static void main(String []args) {
            try{
                MyThread thread=new MyThread();
                thread.start();
                Thread.sleep(2000);
                thread.interrupt();
            }
            catch (InterruptedException e)
            {
                System.out.println("main catch");
                e.printStackTrace();
            }
            System.out.println("end");
    }
}

输出为:

有上面可以得知,即使线程已经是停止状态(伪像),run()方法里面的句子还是会继续执行,为了解决这样的问题,我们使用了抛出异常的方法:

 

下面是一个例子:

MyThread类:
public class MyThread extends Thread {
    public void run(){
        super.run();
        try{
            for(int i=0;i<500000;i++)
            {
                if (this.interrupted()) {
                    System.out.println("线程已经是停止状态了,我要退出了");
                    throw new InterruptedException();
                }
                System.out.println("i="+(i+1));
            }
            System.out.println("我在for的下面,如果我输出,那么就证明线程没有被停止!");
         }
        catch (InterruptedException e){
            System.out.println("进入MyThread.java类run方法中的catch了");
            e.printStackTrace();
        }
    }
}

 

 

test类:
public class test {
        public static void main(String []args) {
            try{
                MyThread thread=new MyThread();
                thread.start();
                Thread.sleep(2000);
                thread.interrupt();
            }
            catch (InterruptedException e)
            {
                System.out.println("main catch");
                e.printStackTrace();
            }
            System.out.println("end");
    }
}

 

输出为:

 

达到了目的。

 

其实直接在run()方法里面通过return()来停止线程。但是代码中出现多个return之后会造成污染,所以不建议使用。

 

public class MyThread extends Thread{
   public void run(){
       while(true)
     {
         if(this.isInterrupted())
          {
           return ;
          }
      }

   }
}

 

 

 

 

 

(正在学习高洪岩先生著的《java多线程编程核心技术》,例子摘自此书)

 

如果有所帮助,脸皮厚求个赞~

此文章仅代表自己(本菜鸟)学习积累记录,或者学习笔记,如有侵权,请联系作者删除。人无完人,文章也一样,文笔稚嫩,在下不才,勿喷,如果有错误之处,还望指出,感激不尽~

技术之路不在一时,山高水长,纵使缓慢,驰而不息。

公众号:秦怀杂货店

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值