Java 学习笔记之 Sleep停止线程
Sleep停止线程:
在Sleep状态下被interrupt,interrupted 状态会被擦除,返回false。
线程在Sleep状态下被interrupt:
public class SleepInterruptThread extends Thread{
@Override
public void run() {
try {
System.out.println("run begin");
Thread.sleep(2000000);
System.out.println("run end");
} catch (InterruptedException e) {
System.out.println("Interrupt in sleep stage. Interrupted status: " + this.isInterrupted());
e.printStackTrace();
}
}
}
public class ThreadRunMain {
public static void main(String[] args) {
testSleepInterruptThread();
}
public static void testSleepInterruptThread(){
try {
SleepInterruptThread sit = new SleepInterruptThread();
sit.start();
Thread.sleep(1000);
sit.interrupt();
} catch (InterruptedException e) {
System.out.println("Main catch");
e.printStackTrace();
}
System.out.println("end!");
}
}
运行结果:

线程在Sleep之前被interrupt:
public class BeforeSleepInterruptThread extends Thread{
@Override
public void run() {
try {
for (int i=0;i<100000;i++){
System.out.println("i="+(i+1));
}
System.out.println("run begin");
Thread.sleep(2000000);
System.out.println("run end");
} catch (InterruptedException e) {
System.out.println("First interrupt, then sleep. Interrupted status: " + this.isInterrupted());
System.out.println("First interrupt, then sleep. Interrupted status: " + Thread.interrupted());
e.printStackTrace();
}
}
}
public class ThreadRunMain {
public static void main(String[] args) {
testBeforeSleepInterruptThread();
}
public static void testBeforeSleepInterruptThread(){
try {
BeforeSleepInterruptThread bsit = new BeforeSleepInterruptThread();
bsit.start();
Thread.sleep(100);
bsit.interrupt();
System.out.println("end!");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
运行结果:

Java 学习笔记之 Sleep停止线程的更多相关文章
- Java 学习笔记之 Stop停止线程
Stop停止线程: 使用stop()方法停止线程是非常暴力的,会抛出java.lang.ThreadDeath Error,但是我们无需显示捕捉, 以下捕捉只是为了看得更清晰. public clas ...
- Java 学习笔记之 Return停止线程
Return停止线程: 使用interrupt()和return结合也可以实现停止线程的效果.不过还是建议使用“抛异常“的方法,因为在catch块中可以将异常向上抛,使线程停止的事件得以传播. pub ...
- Java学习笔记-基础语法Ⅹ-进程线程
学习快一个月了,现在学到了黑马Java教程的300集 打印流的特点: 只负责输出数据,不负责读取数据 有自己的特有方法 字节打印流:PrintStream,使用指定的文件名创建新的打印流 import ...
- java学习笔记15--多线程编程基础2
本文地址:http://www.cnblogs.com/archimedes/p/java-study-note15.html,转载请注明源地址. 线程的生命周期 1.线程的生命周期 线程从产生到消亡 ...
- java学习笔记14--多线程编程基础1
本文地址:http://www.cnblogs.com/archimedes/p/java-study-note14.html,转载请注明源地址. 多线程编程基础 多进程 一个独立程序的每一次运行称为 ...
- Java学习笔记-多线程-创建线程的方式
创建线程 创建线程的方式: 继承java.lang.Thread 实现java.lang.Runnable接口 所有的线程对象都是Thead及其子类的实例 每个线程完成一定的任务,其实就是一段顺序执行 ...
- 《Java学习笔记(第8版)》学习指导
<Java学习笔记(第8版)>学习指导 目录 图书简况 学习指导 第一章 Java平台概论 第二章 从JDK到IDE 第三章 基础语法 第四章 认识对象 第五章 对象封装 第六章 继承与多 ...
- 20145330第六周《Java学习笔记》
20145330第六周<Java学习笔记> . 这周算是很忙碌的一周.因为第六周陆续很多实验都开始进行,开始要准备和预习的科目日渐增多,对Java分配的时间不知不觉就减少了,然而第十和十一 ...
- 并发编程学习笔记(14)----ThreadPoolExecutor(线程池)的使用及原理
1. 概述 1.1 什么是线程池 与jdbc连接池类似,在创建线程池或销毁线程时,会消耗大量的系统资源,因此在java中提出了线程池的概念,预先创建好固定数量的线程,当有任务需要线程去执行时,不用再去 ...
随机推荐
- Prim && Kruskal
Electrification Plan Prim #include<iostream> #include<cstring> using namespace std; cons ...
- BZOJ 1036: [ZJOI2008]树的统计Count(树链剖分+单点更新+区间求和+区间求最大值)
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1036 题意:略. 题解:树链剖分模版,注意一些细节即可. #include <ios ...
- 第10讲-Java集合框架
第10讲 Java集合框架 1.知识点 1.1.课程回顾 1.2.本章重点 1.2.1 List 1.2.2 Set 1.2.3 Map 2.具体内容 2.1.Java集合框架 2.1.1 为什么需要 ...
- 《即时消息技术剖析与实战》学习笔记5——IM系统如何保证消息的一致性
一.什么是消息一致性 消息一致性指的是消息的时序一致性,即消息收发的一致性.如果不能保证时序一致性,就会造成聊天语义不连贯,引起误会. 对于点对点的聊天场景,时序一致性保证接收方的接收顺序和发送方的发 ...
- 数据库常用SQL语句(三):子查询
一.为什么会使用子查询 虽然可以通过连接查询来实现多表查询数据记录,但不建议使用,因为连接查询的性能很差,为什么呢?我们来进行分析,例如 我们要查询部门表t_dept 和雇员表t_employee中的 ...
- 2019CSP初赛基础知识整理
一.硬件 计算机发展: 年代 元件 第一代 1946~1958 电子管 第二代 1959~1964 晶体管 第三代 1965~1970 集成电路 第四代 1971~? 大规模集成电路 世界上第一台 ...
- IDEA中自动导包快捷键
1.File-->Settings 2.解释: 勾选标注第1个选项,IntelliJ IDEA 将在我们书写代码的时候自动帮我们导入需要用到的包.但是对于那些同名的包,还是需要手动 Alt + ...
- windows 服务 安装、卸载
1.新建项目 选中windows服务 2.添加安装程序 3.修改安装代码 ServiceProcessInstaller processInstall; ServiceInstaller servic ...
- [淘宝客技术篇008](无需登录)淘宝天猫优惠券JSON接口1
今天,小星给大家分享的是一个非常重要,非常有意义的接口:获取淘宝天猫优惠券的JSON接口. 先上个链接: http://uland.taobao.com/cp/coupon_list?pid=mm_2 ...
- 纯css实现乌云密布的天气图标
效果 效果如下 实现思路 使用box-shadow属性画几个灰色的圆,将这些圆错落的组合在一起,形成乌云图案 after伪元素写乌云下的投影 增加动画 dom结构 用两个嵌套的div容器,父容器来 ...