详解Java停止线程的四种方法
线程调用interrupt()方法后,在线程的run方法中判断当前对象的interrupted()状态,如果是中断状态则抛出异常,达到中断线程的效果。
如下示例:
MyThread.java
public class MyThread extends Thread { @Override public void run() {try { for (int i = 0; i < 500000; i++) {if (MyThread.interrupted()){ System.out.println('已经是停止状态了,我要退出了!'); throw new InterruptedException();}System.out.println('i = ' + (i+1)); } System.out.println('如果我被输出了,则代表线程没有停止');} catch (InterruptedException e) { System.out.println('在MyThread类中的run方法中被捕获'); e.printStackTrace();} }}
Main.java
/** * 根据中断状态退出for循环 * @Author: xjf * @Date: 2019/5/25 13:27 */public class Main { public static void main(String[] args) {try { MyThread myThread = new MyThread(); myThread.start(); Thread.sleep(100); myThread.interrupt();} catch (InterruptedException e) { e.printStackTrace();}System.out.println('end!'); }}
结果如下:i = 19115i = 19116i = 19117i = 19118i = 19119end!
已经是停止状态了,我要退出了!在MyThread类中的run方法中被捕获java.lang.InterruptedException at com.book.interrupt_exit.MyThread.run(MyThread.java:15)
Process finished with exit code 0
三、停止线程方法2:在沉睡中停止先将线程sleep,然后调用interrupt标记中断状态,interrupt会将阻塞状态的线程中断。会抛出中断异常,达到停止线程的效果。如下示例:
MyThread.java
public class MyThread extends Thread { @Override public void run() {try { System.out.println('run-----------start'); Thread.sleep(5000); System.out.println('run-----------end');} catch (InterruptedException e) { System.out.println('在沉睡中被停止!进入catch,线程的是否处于停止状态:' + this.isInterrupted()); e.printStackTrace();} }}
Main.java
public class Main { public static void main(String[] args) {try { MyThread myThread = new MyThread(); myThread.start(); Thread.sleep(2000); System.out.println('状态:'+MyThread.interrupted()); myThread.interrupt();} catch (InterruptedException e) { e.printStackTrace();} }}
结果run-----------start状态:falsejava.lang.InterruptedException: sleep interrupted在沉睡中被停止!进入catch,线程的是否处于停止状态:false at java.lang.Thread.sleep(Native Method) at com.book.sleep_interrupt.MyThread.run(MyThread.java:13)
线程先调用interrupt标记中断状态,然后线程再睡眠。会抛出中断异常,达到停止线程的效果。如下:
MyThread1.java
public class MyThread1 extends Thread { @Override public void run() {try { for (int i = 0; i < 100000; i++) {System.out.println('i = ' + (i+1)); } System.out.println('run begin'); //interrupt是做一个中断标记,当时不会去中断正在运行的线程,当该线程处于阻塞状态时就会进行中断 //因此,先进行interrupt后,再遇到sleep阻塞时,才会进行中断 Thread.sleep(200000); System.out.println('run end');} catch (InterruptedException e) { System.out.println('先停止,再遇到了sleep! 进入catch!'); e.printStackTrace();} }}
Main1.java
public class Main1 { public static void main(String[] args) {MyThread1 myThread1 = new MyThread1();myThread1.start();myThread1.interrupt();System.out.println('end!'); }}
结果:i = 99993i = 99994i = 99995i = 99996i = 99997i = 99998i = 99999i = 100000run begin
先停止,再遇到了sleep! 进入catch!java.lang.InterruptedException: sleep interrupted at java.lang.Thread.sleep(Native Method) at com.book.sleep_interrupt.MyThread1.run(MyThread1.java:19)
四、停止线程方法3:stop()暴力停止线程调用stop()方法会被暴力停止,方法已弃用。该方法会有不好的后果:
强制让线程停止有可能使一些请理性的工作得不到完成。 对锁定的对象进行了“解锁”,导致数据得不到同步的处理,出现数据不一致的问题(比如一个方法加上了synchronized,并在其中进行了一个长时间的处理,而在处理结束之前该线程进行了stop(),则未完成的数据将没有进行到同步的处理) 五、停止线程方法4:使用return停止线程调用interrupt标记为中断状态后,在run方法中判断当前线程状态,如果为中断状态则return,能达到停止线程的效果。
备注:建议使用“抛异常”的方法来实现线程的停止,因为在catch块中还可以将异常向上抛,使线程停止的事件得以传播
参考:《Java多线程编程核心技术》
到此这篇关于详解Java停止线程的四种方法的文章就介绍到这了,更多相关Java停止线程内容请搜索好吧啦网以前的文章或继续浏览下面的相关文章希望大家以后多多支持好吧啦网!
相关文章:
