做网站的费用如何写分录,西安搬家公司电话附近联系方式,手机wap版,北京做网站设计招聘中断线程 Thread类中interrupt()、interrupted()和isInterrupted()方法 interrupt()方法 其作用是中断此线程#xff08;此线程不一定是当前线程#xff0c;而是指调用该方法的Thread实例所代表的线程#xff09;#xff0c;但实际上只是给线程设置一个中断标志#xff0c…中断线程 Thread类中interrupt()、interrupted()和isInterrupted()方法 interrupt()方法 其作用是中断此线程此线程不一定是当前线程而是指调用该方法的Thread实例所代表的线程但实际上只是给线程设置一个中断标志线程仍会继续运行。 interrupted()方法 作用是测试当前线程是否被中断检查中断标志有标记的话返回一个ture并清除中断状态第二次再调用时中断状态已经被清除将返回一个false。 isInterrupted()方法 作用是只测试此线程(调用者代表的线程)是否被中断 不清除中断状态。 编码
//定义一个MyThread类继承Thread
public class MyThread extends Thread {Overridepublic void run() {for (int i 0; i 10; i) {System.out.println(i(i1));}}
}
//在main方法中测试
public class Do {public static void main(String[] args ) {MyThread threadnew MyThread();thread.start();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 threadnew 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());System.out.println(thread是否存活thread.isAlive());}
}从输出结果看有疑惑为什么后面两个interrupted方法输出的都是false而不是预料中的一个true一个false 注意这是一个坑上面说到interrupted()方法测试的是当前线程是否被中断当前线程当前线程 这里当前线程是main线程而thread.interrupt(中断的是thread线程这里的此线程就是thread线程。 所以当前线程main从未被中断过尽管interrupted()方法是以thread.interrupted()的形式被调用 但它检测的仍然是main线程而不是检测thread线程所以thread.interrupted()在这里相当于main.interrupted()。 再次修改
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-false interrupt()只是打标记若果想要是实现调用interrupt()方法真正的终止线程则可以在线程的run方法中做处理即可比如直接跳出run()方法使线程结束视具体情况而定下面是一个例子。 修改MyThread类
public class MyThread extends Thread {Overridepublic void run() {for (int i 0; i 1000; i) {System.out.println(i(i1));if(this.isInterrupted()){System.out.println(通过this.isInterrupted()检测到中断);System.out.println(第一个interrupted()this.interrupted());System.out.println(第二个interrupted()this.interrupted());break;}}System.out.println(因为检测到中断所以跳出循环线程到这里结束因为后面没有内容了);}
}测试MyThread
public class Do {public static void main(String[] args ) throws InterruptedException {MyThread myThreadnew MyThread();myThread.start();myThread.interrupt();//sleep等待一秒等myThread运行完Thread.currentThread().sleep(1000);System.out.println(myThread线程是否存活myThread.isAlive());}
}总结
interrupt()是给线程设置中断标志interrupted()是检测中断并清除中断状态isInterrupted()只检测中断interrupted()作用于当前线程interrupt()和isInterrupted()作用于调用者线程