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();
}
}
}

运行结果:

最新文章

  1. MYSQL、PHP基础、面向对象基础简单复习总结
  2. 04-c#入门(类型转换)
  3. C++ find 函数用法
  4. wex5 实战 二维码生成,扫描,蓝牙打印
  5. MySQL安装最后一步apply security settings错误
  6. 黄聪:Emeditor 编辑器常用的正则表达式
  7. POJ --- 2918 求解数独
  8. Currency System in Geraldion (Codeforces 560A)
  9. .haccess 配置生效
  10. angular访问后台服务及监控会话超时的封装
  11. Servlet实践--HelloWorld
  12. Android最火热修复实战
  13. 简单的做一个图片上传预览(web前端)
  14. c# 主窗体更新子窗体 进程间通信
  15. 关于iOS开发常用的一些东西
  16. Ubuntu下删除卸载程序图标
  17. QT的初步学习笔记
  18. rsync 实现文件同步 (重要数据通过rsyncr把数据同步到不同的两台服务器上,这样可以防止服务器的硬盘故障导致数据丢失) 客户端同步时如果要排某个目录
  19. Java中++,--,前缀后缀表达值的不同,与^的值计算
  20. bulid-tool

热门文章

  1. CSU 1803 2016 湖南省2016省赛
  2. Fedora7 安装完全过程
  3. SIA-GateWay之API网关安装部署指南
  4. 如何设置FreePBX的数据库用户可以通过远程来连接Mysql数据库?
  5. zabbix -- 学习之一
  6. JSP四大作用域属性范围
  7. Maven依赖配置和依赖范围
  8. git:clone 本地克隆的几种情况
  9. 6、二叉树树(java实现)
  10. Elastic Stack 笔记(二)Elasticsearch5.6 安装 IK 分词器和 Head 插件