php网站开发开发网站教程湖北神润建设工程网站
php网站开发开发网站教程,湖北神润建设工程网站,河南网站制作公司,建网站的网络公司的名称以及服务本篇文章帮大家学习java 如何中断线程#xff0c;包含了Java 如何中断线程使用方法、操作技巧、实例演示和注意事项#xff0c;有一定的学习价值#xff0c;大家可以用来参考。以下实例演示了如何使用interrupt()方法来中断线程并使用 isInterrupted() 方法来判断线程是否已…本篇文章帮大家学习java 如何中断线程包含了Java 如何中断线程使用方法、操作技巧、实例演示和注意事项有一定的学习价值大家可以用来参考。以下实例演示了如何使用interrupt()方法来中断线程并使用 isInterrupted() 方法来判断线程是否已中断public class Main extends Objectimplements Runnable {public void run() {try {System.out.println(in run() - 将运行 work2() 方法);work2();System.out.println(in run() - 从 work2() 方法回来);}catch (InterruptedException x) {System.out.println(in run() - 中断 work2() 方法);return;}System.out.println(in run() - 休眠后执行);System.out.println(in run() - 正常离开);}public void work2() throws InterruptedException {while (true) {if (Thread.currentThread().isInterrupted()) {System.out.println(C isInterrupted() Thread.currentThread().isInterrupted());Thread.sleep(2000);System.out.println(D isInterrupted() Thread.currentThread().isInterrupted());}}}public void work() throws InterruptedException {while (true) {for (int i 0; i int j i * 2;}System.out.println(A isInterrupted() Thread.currentThread().isInterrupted());if (Thread.interrupted()) {System.out.println(B isInterrupted() Thread.currentThread().isInterrupted());throw new InterruptedException();}}}public static void main(String[] args) {Main si new Main();Thread t new Thread(si);t.start();try {Thread.sleep(2000);}catch (InterruptedException x) {}System.out.println(in main() - 中断其他线程);t.interrupt();System.out.println(in main() - 离开);}}运行结果in run() - 将运行 work2() 方法in main() - 中断其他线程in main() - 离开C isInterrupted()truein run() - 中断 work2() 方法
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/bicheng/90176.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!