在上篇文章Java并发编程之线程篇之线程简介(二)中咱们基本了解了如何建立一个线程并执行相应任务,可是并无提到如何中断一个线程。例如:咱们有一个下载程序线程,该线程在没有下载成功以前是不会退出的,假如这个时候用户不想下载了,那咱们该如何中断这个下载线程呢?下面咱们就来学习如何正确的中断一个线程吧。java
对于过期的suspend()、resume()和stop()方法,这里就不介绍了,有兴趣的小伙伴能够查阅相关资料。编程
当咱们须要中断某个线程时,看似咱们只须要调一个中断方法(调用以后线程就不执行了)就好了。可是Java中并无提供一个实际的方法来中断某个线程(不考虑过期的stop()方法),只提供了一个中断标志位,来表示线程在运行期间已经被其余线程进行了中断操做。也就是说线程只有经过自身来检查这个标志位,来判断本身是否被中断了。在Java中提供了三个方法来设置或判断中断标志位,具体方法以下所示:markdown
//类方法,设置当前线程中标志位 public void interrupt() { if (this != Thread.currentThread()) checkAccess(); synchronized (blockerLock) { Interruptible b = blocker; if (b != null) { interrupt0(); // 设置中断标志位 b.interrupt(this); return; } } interrupt0();//设置中断标志位 } //静态方法,判断当前线程是否中断,清除中断标志位 public static boolean interrupted() { return currentThread().isInterrupted(true); } //类方法,判断当前线程是否中断,不清除中断标志位 public boolean isInterrupted() { return isInterrupted(false); } 复制代码
在上述方法中,咱们能够经过interrupt()
来设置相应线程中断标志,经过Thread类静态方法interrupted()
和类方法isInterrupted()
来判断对应线程是否被其余线程中断。并发
其中interrupted()与isInterrupted()方法的主要区别以下:ide
interrupted
判断当前线程是否中断(若是是中断,则会清除中断的状态标志,也就是若是中断了线程,第一次调用这个方法返回true,第二次继续调用则返回false。isInterrupted
判断线程是否已经中断(不清除中断的状态标志)。在上文中,咱们了解了线程的如何设置中断标志位与如何判断标志位,那如今咱们来使用interrupt()
方法来中断一个线程。先看下面这个例子。post
class InterruptDemo { public static void main(String[] args) { Thread thread = new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 1000000; i++) { System.out.println("i=" + i); } } }); thread.start(); try { Thread.sleep(2000); thread.interrupt(); } catch (InterruptedException e) { e.printStackTrace(); } } } //输出结果: i=593210 i=593211 i=593212 i=593213 i=593214 i=593215 i=593216 复制代码
运行上述代码,观察输出结果,咱们发现线程并无被终止,缘由是由于interrupt()方法只会设置线程中断标志位,并不会真正的中断线程
。也就是说咱们只有本身来判断线程是否终止。通常状况下,当咱们检查到线程被中断(也就是线程标志位为true)时,会抛出一个InterruptedException
异常,来中断线程任务。具体代码以下:学习
class InterruptDemo { public static void main(String[] args) { Thread thread = new Thread(new Runnable() { @Override public void run() { try { for (int i = 0; i < 1000000; i++) { if (Thread.interrupted()) { System.out.println("检测到线程被中断"); throw new InterruptedException(); } System.out.println("i=" + i); } } catch (InterruptedException e) { //执行你本身的中断逻辑 System.out.println("线程被中断了,你本身判断该如何处理吧"); e.printStackTrace(); } } }); thread.start(); try { Thread.sleep(2000); thread.interrupt(); } catch (InterruptedException e) { e.printStackTrace(); } } } //输出结果: i=218626 i=218627 i=218628 i=218629 i=218630 检测到线程被中断 线程被中断了,你本身判断该如何处理吧 java.lang.InterruptedException at InterruptDemo$1.run(InterruptDemo.java:18) at java.base/java.lang.Thread.run(Thread.java:835) 复制代码
在上述代码中,咱们经过在线程中判断Thread.interrupted()
来判断线程是否中断,当线程被中断后,咱们抛出InterruptedException异常。而后经过try/catch来捕获该异常来执行咱们本身的中断逻辑。固然咱们也能够经过Thread.currentThread().isInterrupted()
来判断。这两个方法的区别已经在上文介绍了,这里就不过多的介绍了。this
在上文中提到的使用interrupt()来中断线程之外,咱们还能够经过一个boolean来控制是否中断线程。具体例子以下所示:spa
class InterruptDemo { public static void main(String[] args) { RunnableA runnableA = new RunnableA(); Thread thread = new Thread(runnableA); thread.start(); try { Thread.sleep(2000); runnableA.interruptThread(); } catch (InterruptedException e) { e.printStackTrace(); } } static class RunnableA implements Runnable { boolean isInterrupt; @Override public void run() { for (int i = 0; i < 1000000; i++) { if (!isInterrupt) { System.out.println("i=" + i); } else { System.out.println("线程结束运行了"); break; } } } void interruptThread() { isInterrupt = true; } } } //输出结果: i=240399 i=240400 i=240401 i=240402 i=240403 i=240404 i=240405 线程结束运行了 复制代码
上述代码中,咱们经过判断isInterrupt
的值来判断是否跳出循环。run()方法的结束,就标志着线程已经执行完毕了。线程
站在巨人的肩膀上,才能看的更远~