这里主要探讨中断常用的三个方法:

主要使用的阻塞三个方法:
==注意:interrupt()不能中断在运行中的线程,它只能改变中断状态而已。实际完成的是让受阻塞的线程退出阻塞状态。==
确切的说:是被三种方法之一阻塞时,调用该线程的interrupt()方法,那么线程将抛出一个个InterruptedException中断异常,从而提早地终结被阻塞状态。
public class Runner3 implements Runnable {
@Override
public void run() {
while (true) {
if (Thread.currentThread().isInterrupted()) {
System.out.println("我进入中断了,但我还在跑");
} else {
System.out.println("我没有进入中断");
}
}
}
public static void main(String[] args) {
Runner3 runner3 = new Runner3();
Thread thread3 = new Thread(runner3);
thread3.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
thread3.interrupt();
}
}
输出结果大致如下:
我没有进入中断
我没有进入中断
我进入中断了,但我还在跑
我进入中断了,但我还在跑
我进入中断了,但我还在跑
...
这里看到,执行interrupt()后,对线程执行中断后依然在执行,线程依然在运行。
我们调整一下run方法
public void run() {
while (true) {
if (Thread.currentThread().isInterrupted()) {
System.out.println("我进入中断了,但我还在跑");
Thread.interrupted();//重置状态
} else {
System.out.println("我没有进入中断");
}
}
}
输出结果如下:
我没有进入中断
我没有进入中断
我进入中断了,但我还在跑
我没有进入中断
我没有进入中断
...
这里看到中断的状态重置了,那么我们如何去应用这个中断状态呢?
案例1
public class Runner3 implements Runnable {
@Override
public void run() {
while (true) {
if (Thread.currentThread().isInterrupted()) {
System.out.println("我进入中断了,但我还在跑");
//
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("2"+Thread.currentThread().isInterrupted());
//输出false
}
} else {
System.out.println("我没有进入中断");
}
}
}
public static void main(String[] args) {
Runner3 runner3 = new Runner3();
Thread thread3 = new Thread(runner3);
thread3.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
thread3.interrupt();
}
}
执行上面的代码,我们可以看到在抛出异常后,Thread.currentThread().isInterrupted()输出为false,证明线程的中断状态已经复位了。
另外因为我们是先执行了interrupt()然后再进入睡眠状态,但是依然抛出了异常。
因为Object#wait方法会阻塞线程,所以当我们执行interrupt时,会抛出InterruptedException异常。
那么Object#wait方法阻塞线程会导致的差异在哪里?
==最主要的差别在于sleep方法没有释放锁,而wait方法释放了锁,使得其它线程可以使用同步控制块或者方法。==