线程interrupted方法:

interrupted()是Thread类的方法,用来测试当前线程是否已经中断。

public class InterruptThread extends Thread{
@Override
public void run() {
for (int i = 0; i< 5000000; i++){
System.out.println("i=" + (i + 1));
}
}
} public class ThreadRunMain {
public static void main(String[] args) {
testInterruptThread();
} public static void testInterruptThread(){
try {
InterruptThread it = new InterruptThread();
it.start();
Thread.sleep(1000);
it.interrupt();
System.out.println("First call: " + Thread.interrupted());
System.out.println("Second call: " + Thread.interrupted());
} catch (InterruptedException e) {
System.out.println("Main catch");
e.printStackTrace();
}
System.out.println("end!");
}
}

运行结果:

从控制台打印的结果来看,返回的结果是false,因为当前线程是main,被中断的却是InterruptThread,所以main线程不受影响。

再看一个例子:

public class ThreadRunMain {
public static void main(String[] args) {
testMainInterruptThread();
} public static void testMainInterruptThread(){
Thread.currentThread().interrupt();
System.out.println("First call: " + Thread.interrupted());
System.out.println("Second call: " + Thread.interrupted());
System.out.println("end!");
}
}

运行结果:

同样是调用Thread.interrupted(), 但是为什么第一次结果是true,第二次确是false呢?

因为interrupted()方法具有清除状态功能,所以第二次调用interrupted()返回的值是false。

Java 学习笔记之 线程interrupted方法的更多相关文章

  1. Java 学习笔记之 线程interrupt方法

    线程interrupt方法: interrupt方法是用来停止线程的,但是他的使用效果并不像for+break那样,马上就停止循环. 调用interrupt()其实仅仅是在当前线程中打了一个停止标记, ...

  2. Java 学习笔记之 线程isInterrupted方法

    线程isInterrupted方法: isInterrupted()是Thread对象的方法,测试线程是否已经中断. public class ThreadRunMain { public stati ...

  3. Java 学习笔记之 线程sleep方法

    线程sleep方法: 单主线程使用sleep: Main线程差了2000毫秒. public class MainSleepThread extends Thread{ @Override publi ...

  4. 0040 Java学习笔记-多线程-线程run()方法中的异常

    run()与异常 不管是Threade还是Runnable的run()方法都没有定义抛出异常,也就是说一条线程内部发生的checked异常,必须也只能在内部用try-catch处理掉,不能往外抛,因为 ...

  5. Java 学习笔记之 线程isAlive方法

    isAlive方法: 方法isAlive()功能是判断当前线程是否处于活动状态. 活动状态就是线程启动且尚未终止,比如正在运行或准备开始运行. public class IsAliveThread e ...

  6. java学习笔记15--多线程编程基础2

    本文地址:http://www.cnblogs.com/archimedes/p/java-study-note15.html,转载请注明源地址. 线程的生命周期 1.线程的生命周期 线程从产生到消亡 ...

  7. java学习笔记14--多线程编程基础1

    本文地址:http://www.cnblogs.com/archimedes/p/java-study-note14.html,转载请注明源地址. 多线程编程基础 多进程 一个独立程序的每一次运行称为 ...

  8. java学习笔记之线程(Thread)

    刚开始接触java多线程的时候,我觉得,应该像其他章节的内容一样,了解了生命周期.构造方法.方法.属性.使用的条件,就可以结束了,然而随着我的深入学习了解,我发现java的多线程是java的一个特别重 ...

  9. JAVA学习笔记16——线程生命周期

    当线程被创建并启动以后,它既不是一启动就进入了执行状态,也不是一直处于执行状态,在线程的生命周期中,它要经过新建(New).就绪(Runnable).运行(Running).阻塞(Blocking)和 ...

随机推荐

  1. POJ 3067 Japan (树状数组求逆序对)

    POJ - 3067 题意:有(1-n)个城市自上到下在左边, 另有(1-m)个城市自上到下在右边,共有m条高速公路,现求这m条直线的交点个数,交点不包括在城市处相交. 题解:先将高速公路读入,然后按 ...

  2. Rikka with Prefix Sum(组合数学)

    Rikka with Prefix Sum 题目描述 Prefix Sum is a useful trick in data structure problems. For example, giv ...

  3. codeforces 807 D. Dynamic Problem Scoring(贪心+思维)

    题目链接:http://codeforces.com/contest/807/problem/D 题意:对于动态计分的 Codeforces Round ,已知每题的 score 是根据 Round ...

  4. Android-友盟第三方登录与分享

    ### 前言 最近项目中又一次需要集成友盟的三方登录与分享,之前没有记录过,所以这次来写一下... ### 准备工作 1.注册友盟账号创建应用,获取key:申请地址http://www.umeng.c ...

  5. 自动化专业如何转SLAM或机器学习岗?

    由于不方便放链接,更好的阅读体验请查看:自动化专业如何转SLAM或机器学习岗? 本文来自知乎上的同名问题,原文链接: https://www.zhihu.com/question/266685012/ ...

  6. 工作中遇到的99%SQL优化,这里都能给你解决方案(二)

    -- 示例表 CREATE TABLE `employees` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(24) NOT NULL ...

  7. Python基础:Python运行的两种基本方式

    完成Python的安装之后,我们可以开始编写Python代码以及运行Python程序了.我们来看一下运行Python具体有哪几种方式 1.REPL 所谓REPL即read.eva.print.loop ...

  8. 我真的不想再用 JPA 了

    在开发者的圈子里,没当说到一种技术好或者不好,都会引发激烈或者不激烈的争论,直到一个开发者出来说 PHP 是世界上最好的语言,大家伙儿才会纷纷退去继续写代码. 今天说 JPA 的问题不是想引发什么讨论 ...

  9. .Net基础篇_学习笔记_第五天_流程控制do-while循环

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  10. IOS13系统升级带来的H5兼容性问题

    20号新推送的IOS13给很多app厂商和RD带来了便秘的感觉,目前复现的问题如下,后续还会持续更新: 1.H5 hybrid输入框导致的页面上移,卡住不动.收起减半后,页面出现半截白屏.(IOS12 ...