被调用的方式:
interrupt和isInterrupted 是Thread类普通方法,被实例对象调用,都是非静态方法,也可以用线程对象来访问,例如t.interrupt(),t.isInterrupted()。
interrupted是Thread类中的静态方法,可以通过类名形式来访问:Thread.interrupted()
概念区分:(注意留意this和current)
interrupt()方法:中断此线程(原文:Interrupts this thread)(此线程不一定是当前线程,而是指调用该方法的Thread实例所代表的线程)
interrupted()方法:测试当前线程是否被中断(检查中断标志)原文:Tests whether the current thread has been interrupted,返回一个boolean并清除中断状态,第二次再调用时中断状态已经被清除,将返回一个false。
isInterrupted()方法:测试此线程是否被中断 ,不清除中断状态(原文:Tests whether this thread has been interrupted)
实践练习:
1、interrupt()不能中断正在运行中的线程,只能改变中断状态。
public class InterruptionTestForJava implements Runnable{
public static void main(String[] args) throws InterruptedException {
Thread testThread = new Thread(new InterruptionTestForJava(), "InterruptionInJava");
testThread.start();
Thread.sleep(1000);
//interrupt()不能中断在运行中的线程,它只能改变中断状态而已。
testThread.interrupt();
System.out.println("main end");
}
@Override
public void run() {
while (true){
if (Thread.currentThread().isInterrupted()){
System.out.println("yes ,i am interrupted,but i am still running");
}else {
System.out.println("not yet interrupted");
}
}
}
运行main方法发现程序没有结束,在打印完“not yet interrupted”后,继续疯狂打印“yes ,i am interrupted,but i am still running”
2、那么如何正确的中断?一种处理方式是在中断状态被修改后,直接返回return
public class InterruptionTestForJava implements Runnable{
public static void main(String[] args) throws InterruptedException {
Thread testThread = new Thread(new InterruptionTestForJava(), "InterruptionInJava");
testThread.start();
Thread.sleep(1000);
//interrupt()不能中断在运行中的线程,它只能改变中断状态而已。
testThread.interrupt();
System.out.println("main end");
}
@Override
public void run() {
while (true){
if (Thread.currentThread().isInterrupted()){
System.out.println("yes ,i am interrupted,but i am still running");
return;
}else {
System.out.println("not yet interrupted");
}
}
}
输出如下:
not yet interrupted
……
not yet interrupted
not yet interrupted
not yet interrupted
not yet interrupted
main end
yes ,i am interrupted,but i am still running
3、“优雅”的处理方式——无阻塞情况(无sleep、wait、join方法调用)
public class InterruptionTestForJava implements Runnable{
//如何中断线程?通过添加一个开关来做到
private static volatile boolean on = false;
public static void main(String[] args) throws InterruptedException {
Thread testThread = new Thread(new InterruptionTestForJava(), "InterruptionInJava");
testThread.start();
Thread.sleep(1000);
//interrupt()不能中断在运行中的线程,它只能改变中断状态而已。
testThread.interrupt();
InterruptionTestForJava.on = true;
System.out.println("main end");
}
//无线程阻塞的情况下可以通过一个开关
@Override
public void run() {
while (!on){
if (Thread.currentThread().isInterrupted()){
System.out.println("yes ,i am interrupted,but i am still running");
return;
}else {
System.out.println("not yet interrupted");
}
}
}
}
4、“优雅”的处理方式——有阻塞情况
public class InterruptionTestForJava implements Runnable{
//有线程阻塞情况时如何中断线程?考虑通过一个开关来实现
private static volatile boolean on = false;
public static void main(String[] args) throws InterruptedException {
Thread testThread = new Thread(new InterruptionTestForJava(), "InterruptionInJava");
testThread.start();
Thread.sleep(1000);
//interrupt()不能中断在运行中的线程,它只能改变中断状态而已。
InterruptionTestForJava.on = true;
testThread.interrupt();
System.out.println("main end");
}
//如果有线程阻塞情况就无法通过开关变量来处理,通过interrupt函数可以迅速中断被阻塞的线程,抛出一个InterruptedException,将线程从阻塞状态解救出来
@Override
public void run() {
while (!on){
try {
Thread.sleep(100000);
} catch (InterruptedException e) {
System.out.println("Caught exception right now :" + e);
}
}
}
}
感谢此篇文章:https://blog.csdn.net/zhuyong7/article/details/80852884