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

java线程控制、状态同步、volatile、Thread.interupt以及ConcurrentLinkedQueue在有些严格的系统中 我们需要做到干净的停止线程并清理相关状态 涉及到这个主题会带出很多的相关点 简单的总结如下 我们知道 在 java 中 有一个 volatile 关键字 其官方说明 https docs oracle com javase tutorial essential concurrency atomic html 为 Using nbsp volatile nbsp variablesred

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

我们知道,在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.

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/210861.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 下午11:44
下一篇 2026年3月18日 下午11:45


相关推荐

  • 台式计算机连不上网,台式机插上网线连不上网怎么办?[通俗易懂]

    台式计算机连不上网,台式机插上网线连不上网怎么办?[通俗易懂]由于你提供的问题描述不够详细具体,可能性太多了;这里为你分析下常见的一些情况以供参考!1.要确定是不是只有这一个台式机不能上网,同一个路由器下的其他终端是可以上网的。如果是这样,那基本和路由器设置无关(特殊情况例外)。2.台式机插上网线后,本地连接是否正常link起来?如果本地连接为一个“×”,那说明没有link成功,检查台式机的网卡驱动是否安装正常。由于此时台式机上不了网,建议你通过其他可以…

    2022年6月26日
    31
  • OpenClaw桌面版安装指南

    OpenClaw桌面版安装指南

    2026年3月12日
    3
  • ipfs是什么挖矿机制(目前最好的ipfs矿机)

    尽管近期市场正在复苏,但FIL仍在50U左右波动,并未出现太大波动,许多投资者都望而却步。IPFS/FIL不错。为什么货币价格不上涨?这种现象其实已经不正常了,价格的波动和数据的修改都不可能在正常的情况下处于正常的状态,相信华雷新山不会出现任何数字货币的不涨不跌,以前的程序员10年5月btc1万现在10。1000个比特币相当于4亿u。11年过去了,现在还不知道哪位程序员。btc用了将近11年的时间才证明了它的价值。对今天的大多数普通人来说,这是一个不能实现的梦想。eth的概念最初是Vita.

    2022年4月14日
    69
  • phpstorm 2021.11.3 激活码(JetBrains全家桶)[通俗易懂]

    (phpstorm 2021.11.3 激活码)JetBrains旗下有多款编译器工具(如:IntelliJ、WebStorm、PyCharm等)在各编程领域几乎都占据了垄断地位。建立在开源IntelliJ平台之上,过去15年以来,JetBrains一直在不断发展和完善这个平台。这个平台可以针对您的开发工作流进行微调并且能够提供…

    2022年3月30日
    55
  • 基于微软嵌入式系统添加语音功能(语音合成与语音识别)「建议收藏」

    基于微软嵌入式系统添加语音功能(语音合成与语音识别)「建议收藏」基于微软嵌入式系统添加语音功能(语音合成与语音识别)

    2022年6月26日
    28
  • (数据库)数据库分类

    (数据库)数据库分类1.面向操作的关系型数据库典型性应用领域:ERP,CRM,信用卡交易,中小型电商数据储存方法:表格流行厂商:OracleDatabase,MicrosoftSQLServer,IBMDB2,EnterpriseDB(PostgreSQL),MySQL优点:完善的生态环境保护,事务保证/数据一致性缺点:严苛的数据模型界定,数据库拓展限制,和非结构型的结合应用较难。2.面向数据分析的关系型数据库典型性应用领域:数据仓库,商务智能,数据科学研究数据储存方法:表格流行厂商:OracleE

    2022年6月24日
    27

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号