java isInterrupted()判断线程的实例讲解
作者:小妮浅浅 发布时间:2023-07-21 01:45:53
标签:java,isInterrupted(),判断线程
1、说明
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);
}
}
}
来源:https://www.py.cn/java/jichu/30260.html
0
投稿
猜你喜欢
- AOP概念的引入传统的登录原理:如上图所示这是一个基本的登录原理图,但是如果我们想要在这个登录之上添加一些新的功能,比如权限校验那么我们能想
- 0. 困扰很久的问题Android控件的宽和高保持比例,这是从我接触Android以来,一直不断会遇到的需求。以前,要么就是在代码里直接设置
- Result也是Struts2比较重要的一部分,在Result的配置中常用的有四种类型:dispatcher、redirect、chain和
- 本文实例讲述了C#写入对象或集合类型数据到xml文件的方法。分享给大家供大家参考。具体实现方法如下:public static string
- 1.1 JDK 14详细概述JDK 8 已经在 2014年 3月 18日正式可用,JDK 8作为长期支持(Long-Term-Support
- java synthetic关键字。有synthetic标记的field和method是class内部使用的,正常的源代码里不会出现synt
- 本文实例讲述了Android仿百度谷歌搜索自动提示框AutoCompleteTextView简单应用。分享给大家供大家参考,具体如下:现在我
- 一、背景知识:树(Tree)在之前的笔记中,我们介绍的链表、栈、队列、数组和字符串都是以线性结构来组织数据的。本篇笔记要介绍的树采用的是树状
- 首先对Servlet上传文件的简单理解此前,Servlet本身没有对文件上传提供直接的支持,一般需要使用第三方框架来实现,这样就比较麻烦不过
- 问题描述在使用mybatis进行开发的时候,由于可以动态拼接sql,这样大大方便了我们。但是也有一定的问题,当我们动态sql拼接的块很多的时
- HashMap相同key累加valueimport java.util.HashMap;import java.util.Map;publi
- using System;using System.Collections.Generic;using System.Globalizati
- 最近项目中使用了 MyBatis-Plus,点击看官方文档。使用一个新的框架,首先是验证框架的使用。 使用 MyBatis-Plu
- 什么是Handler Handler是Android消息机制的上层接口,它为我们封装了许多底层的细节,让
- try &
- 在上篇文章给大家介绍了Android Studio 3.6 正式版终于发布了,快来围观,需要的朋友可以点击查看,今天给大家分享我安装Andr
- 实例如下:static bool CheckPowerOfTwo(ulong num){ return num > 0 &
- 布局布局效果如下,下面每个“网格”都是一个按钮,点击按钮,就会有相应的事件发生。由于UniformG
- Android通过scheme跳转界面,应该如何实现?需求通过后台返回链接地址eg: app://com.bobo.package/path
- 问题描述设有n个会议的集合C={1,2,…,n},其中每个会议都要求使用同一个资源(如会议室),而在同一时间内只能有一