在有些严格的系统中,我们需要做到干净的停止线程并清理相关状态。涉及到这个主题会带出很多的相关点,简单的总结如下:

我们知道,在java中,有一个volatile关键字,其官方说明(https://docs.oracle.com/javase/tutorial/essential/concurrency/atomic.html)为:

Using volatile variables reduces the risk of memory consistency errors, because any write to a volatile variable establishes a happens-before relationship with subsequent reads of that same variable. This means that changes to a volatile variable are always visible to other threads. What's more, it also means that when a thread reads a volatilevariable, it sees not just the latest change to the volatile, but also the side effects of the code that led up the change.

基本可以认为,写入volatile变量的值对于其他线程总是可见,一个线程读取到volatile变量时,不仅其值是最新的,其他使用改变了判断的代码也会同时级联发生变化。

不过,volatile的范围仅限于每次从堆读取该值时,进入方法体内后,如果执行的时间较长或者一直处于循环,比如线程的run()方法内,期间对volatile的变更对方法体是不可见的,所以这一点需要注意,如果一定要有效,那就再包装个方法。

如果不想要再加个方法,可以考虑使用ConcurrentLinkedQueue,可以一开始为空,后面轮询判断status.poll() == null的方式实现。

第三,我们知道Thread有stop和interupt这两个终止线程的方法。因为stop已经@Deprecated,所以假设不会使用。再看interupt的副作用,对于很多程序比如log4j来说,其实使用interupt并无副作用,但有些情况下,使用interrupt会导致不必要的系统不一致性,看interrupt的javadoc,如下:

Interrupts this thread.

Unless the current thread is interrupting itself, which is always permitted, the checkAccess method of this thread is invoked, which may cause a SecurityException to be thrown.

If this thread is blocked in an invocation of the wait()wait(long), or wait(long, int) methods of the Object class, or of the join()join(long)join(long, int)sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.

If this thread is blocked in an I/O operation upon an interruptible channel then the channel will be closed, the thread's interrupt status will be set, and the thread will receive a ClosedByInterruptException.

If this thread is blocked in a Selector then the thread's interrupt status will be set and it will return immediately from the selection operation, possibly with a non-zero value, just as if the selector's wakeup method were invoked.

If none of the previous conditions hold then this thread's interrupt status will be set.

Interrupting a thread that is not alive need not have any effect.

所以一般应使用前两种方法。

java线程控制、状态同步、volatile、Thread.interupt以及ConcurrentLinkedQueue相关推荐

  1. java线程的状态及状态间的切换

    在 Java 5 以后,线程状态被明确定义在其公共内部枚举类型 java.lang.Thread.State 中. 分别是: 1.        NEW(初始化状态) 2.        RUNNAB ...

  2. 一张图弄懂java线程的状态和生命周期

    转载自 一张图弄懂java线程的状态和生命周期 上图是一个线程的生命周期状态流转图,很清楚的描绘了一个线程从创建到终止的过程. 这些状态的枚举值都定义在java.lang.Thread.State下 ...

  3. Java多线程基础学习,Thread解读、java线程的状态、同步和异步、两阶段终止模式

    理论概述 单线程和多线程 为什么要使用多线程呢?多线程有什么好处呢? 如果在程序中,需要读写一个文件,该文件很大,那我们执行到该io操作时,cpu就会等待该io操作执行完才会继续运行下面的代码,进程调 ...

  4. java线程主要状态及转换_Java线程状态转换及控制

    线程的状态(系统层面) 一个线程被创建后就进入了线程的生命周期.在线程的生命周期中,共包括新建(New).就绪(Runnable).运行(Running).阻塞(Blocked)和死亡(Dead)这五 ...

  5. Java线程池状态判断源码_深入浅出Java线程池:源码篇

    前言 在上一篇文章深入浅出Java线程池:理论篇中,已经介绍了什么是线程池以及基本的使用.(本来写作的思路是使用篇,但经网友建议后,感觉改为理论篇会更加合适).本文则深入线程池的源码,主要是介绍Thr ...

  6. Java 线程的状态

    线程状态简介   经典的线程五态模型,有五种状态:创建.就绪.执行.阻塞.终止.   而 Java 的线程状态分为了六种状态:NEW.RUNNABLE.BLOCKED.WAITING.TIMED_WA ...

  7. JAVA线程六种状态_Java:线程的六种状态及转化

    多线程概述及创建方式 Java:线程的六种状态及转化 关于线程的生命周期,网上书上说法不一,难以统一,本篇做一个总结: java.lang.Thread.State枚举类中定义了六种线程的状态,可以调 ...

  8. Java线程详解(10)-volatile关键字

    Java 语言中的 volatile 变量可以被看作是一种 "程度较轻的 synchronized":与 synchronized 块相比,volatile 变量所需的编码较少,并 ...

  9. 【Java并发编程 】同步——volatile 关键字

    英 /ˈvɒlətaɪl/ 我了太噢(记不住单词怎么读) 一.volatile的介绍? volatile是一个轻量级的synchronized,一般作用与变量,在多处理器开发的过程中保证了内存的可见性 ...

最新文章

  1. 「UI 测试自动化selenium」汇总
  2. [转] apache2: bad user name ${APACHE_RUN_USER}
  3. SQL_Server2008数据连接池
  4. 办公软件Office 2010下载安装介绍
  5. 你不知道的 JavaScript 笔记——作用域和闭包
  6. android studio怎样运行uniapp打包项目_uni app系列002:离线打包apk(2)
  7. 计算机运维知识题库,(哭求哥哥姐姐帮助一下)计算机维护笔试题库(辛苦了)...
  8. python网页爬虫例子_Python网络爬虫 - 一个简单的爬虫例子
  9. 单片机c语言应用100例第3版课后答案,单片机C语言应用100例(第3版)(含光盘1张)...
  10. 使用run-rs启动mongodb
  11. Django之序列化
  12. React中添加class——借助第三方库classnames
  13. [转载] python模块的分类有哪些_整理了一份清单,常见Python问题的快速解答包
  14. 《Ray Tracing from the Ground Up》中的坐标系是怎么确定的
  15. 深度学习中所需的线性代数知识
  16. ubuntu server安装的一些坑
  17. 计算机所建造全过程,Midas 桥梁设计建模计算,全过程图文解析!
  18. UI - PS如何导入不同的字体
  19. 计算机网络技术 虚拟仿真教学实验,虚拟仿真实验教学优秀课件
  20. 一个被数字化的职场技术人

热门文章

  1. 防火墙gre over ipsec实验
  2. 【C++】命名空间namespace详解
  3. 按键全部消失!瀑布屏将成旗舰新标杆,OPPO、vivo你看好谁
  4. mysql8.0.28下载安装教程(win10),一键安装,超详细
  5. 深度学习中的Adam优化算法详解
  6. 腾讯蓝鲸智云版本再迎新升级,正式推出容器化部署版本V7.0
  7. 一名数据分析师到算法工程师的转岗经历
  8. 查看oracle操作历史记录
  9. 用python画颗爱心祝生日快乐_python图特尔图书馆写“生日快乐”,Pythonturtle,库写...
  10. element-ui生成二维码