Thread类中interrupt()、interrupted()和isInterrupt。。。⾸先看看官⽅说明:
interrupt()⽅法
其作⽤是中断此线程(此线程不⼀定是当前线程,⽽是指调⽤该⽅法的Thread实例所代表的线程),但实际上只是给线程设置⼀个中断标志,线程仍会继续运⾏。
interrupted()⽅法
作⽤是测试当前线程是否被中断(检查中断标志),返回⼀个boolean并清除中断状态,第⼆次再调⽤时中断状态已经被清除,将返回⼀个fal。
isInterrupted()⽅法
作⽤是只测试此线程是否被中断 ,不清除中断状态。
下⾯我们进⾏测试说明:
定义⼀个MyThread类,继承Thread,如下:
public class MyThread extends Thread {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("i="+(i+1));
}
think about
}
}
在main⽅法中测试:
public class Do {
public static void main(String[] args ) {
MyThread thread=new MyThread();
thread.start();
tomato的音标
thread.interrupt();
System.out.println("第⼀次调⽤thread.isInterrupted():"+thread.isInterrupted());
System.out.println("第⼆次调⽤thread.isInterrupted():"+thread.isInterrupted());
System.out.println("thread是否存活:"+thread.isAlive());
}
}
输出如下:
从结果可以看出调⽤interrupt()⽅法后,线程仍在继续运⾏,并未停⽌,但已经给线程设置了中断标志,两个isInterrupted()⽅法都会输出true,也说明isInterrupted()⽅法并不会清除中断状态。
下⾯我们把代码修改⼀下,多加两⾏调⽤interrupted()⽅法:
public class Do {
public static void main(String[] args ) {
MyThread thread=new MyThread();
thread.start();
thread.interrupt();
System.out.println("第⼀次调⽤thread.isInterrupted():"+thread.isInterrupted());
System.out.println("第⼆次调⽤thread.isInterrupted():"+thread.isInterrupted());
//测试interrupted()函数
System.out.println("第⼀次调⽤thread.interrupted():"+thread.interrupted());
System.out.println("第⼆次调⽤thread.interrupted():"+thread.interrupted());
ifxSystem.out.println("thread是否存活:"+thread.isAlive());
}
}
输出如下:
从输出结果看,可能会有疑惑,为什么后⾯两个interrupted⽅法输出的都是fal,⽽不是预料中的⼀个true⼀个fal?注意这是⼀个坑上⾯说到,interrupted()⽅法测试的是当前线程是否被中断,当前线程当前线程这⾥当前线程是main线程,⽽thread.interrupt()中断的是thread线程,这⾥的此线程就是thread线程。所以当前线程main从未被中断过,尽管interrupted()⽅法是以thread.interrupted()的形式被调⽤,但它检测的仍然是main线程⽽不是检测thread线程,所以thread.interrupted()在这⾥相当于main.interrupted()。对于这点,下⾯我们再修改进⾏测试。
Thread.currentThread()函数可以获取当前线程,下⾯代码中获取的是main线程
packinglist
public class Do {
public static void main(String[] args ) throws InterruptedException {
Thread.currentThread().interrupt();
System.out.println("第⼀次调⽤Thread.currentThread().interrupt():"
+Thread.currentThread().isInterrupted());
System.out.println("第⼀次调⽤thread.interrupted():"
+Thread.currentThread().interrupted());
System.out.println("第⼆次调⽤thread.interrupted():"
+Thread.currentThread().interrupted());
}
}
这⾥都是针对当前线程在操作,如果interrupted()⽅法有检测中断并清除中断状态的作⽤,预料中的输出应该是true-true-fal,实际输出如下:
结果证明猜想是正确的。
若果想要是实现调⽤interrupt()⽅法真正的终⽌线程,则可以在线程的run⽅法中做处理即可,⽐如直接跳出run()⽅法使线程结束,视具体情况⽽定,下⾯是⼀个例⼦。
修改MyThread类:
public class MyThread extends Thread {
@Override
public void run() {十分钟看懂中国
for (int i = 0; i < 1000; i++) {
System.out.println("i="+(i+1));
if(this.isInterrupted()){
System.out.println("通过this.isInterrupted()检测到中断");
System.out.println("第⼀个interrupted()"+this.interrupted());
System.out.println("第⼆个interrupted()"+this.interrupted());
break;
wmc}
四级成绩什么时候出来的2021}
disneyland怎么读System.out.println("因为检测到中断,所以跳出循环,线程到这⾥结束,因为后⾯没有内容了");
}
}
lcds测试MyThread:
public class Do {
public static void main(String[] args ) throws InterruptedException {
MyThread myThread=new MyThread();
myThread.start();
myThread.interrupt();
//sleep等待⼀秒,等myThread运⾏完
Thread.currentThread().sleep(1000);
举行的英文
System.out.println("myThread线程是否存活:"+myThread.isAlive());
}
}
结果:
最后总结,关于这三个⽅法,interrupt()是给线程设置中断标志;interrupted()是检测中断并清除中断状态;isInterrupted()只检测中断。还有重要的⼀点就是interrupted()作⽤于当前线程,interrupt()和isInterrupted()作⽤于此线程,即代码中调⽤此⽅法的实例所代表的线程。