java isInterrupted()判断线程的实例讲解
isInterrupted()可以判断当前线程是否被中断,仅仅是对interrupt()标识的一个判断,并不会影响标识发生任何改变(因为调用interrupt()的时候会设置内部的一个叫interrupt flag的标识)。
2、实例public static void main(String[] args) throws InterruptedException{ Thread thread = new Thread(()->{while (true){} }); thread.start(); TimeUnit.SECONDS.sleep(1); System.out.println('Thread is interrupted :'+thread.isInterrupted()); thread.interrupt(); System.out.println('Thread is interrupted :'+thread.isInterrupted());}
实例扩展补充:
ublic class t12 { public static void main(String[] args) {try { MyThread12 thread = new MyThread12(); thread.start(); Thread.sleep(500); thread.interrupt(); System.out.println('是否终止1? =' + thread.interrupted()); System.out.println('是否终止2? =' + thread.interrupted());} catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace();}System.out.println('-------------end-------------'); }}class MyThread12 extends Thread { public void run() {for (int i = 0; i < 50000; i++) { System.out.println('i = ' + i);} }}
到此这篇关于java isInterrupted()判断线程的实例讲解的文章就介绍到这了,更多相关java isInterrupted()如何判断线程内容请搜索好吧啦网以前的文章或继续浏览下面的相关文章希望大家以后多多支持好吧啦网!
相关文章:
1. Springboot web项目打包实现过程解析2. 关于springboot 中使用httpclient或RestTemplate做MultipartFile文件跨服务传输的问题3. python 网络编程要点总结4. Python 利用flask搭建一个共享服务器的步骤5. Python Django 后台管理之后台模型属性详解6. ASP.NET Core 5.0中的Host.CreateDefaultBuilder执行过程解析7. 利用php来自动调用不同服务器上的flash8. asp.net core 中的Jwt(Json Web Token)的使用详解9. vue+element+oss实现前端分片上传和断点续传10. 详解CSS伪元素的妙用单标签之美
