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

我们知道,在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. Thread线程源码解析,Java线程的状态,线程之间的通信

    线程的基本概念 什么是线程 现代操作系统在运行一个程序的时候,会为其创建一个进程.例如,启动一个Java程序,操作系统就会创建一个Java进程.线代操作系统调度的最小单位是线程.也叫做轻量级进程.在一 ...

  2. Java线程的状态

    Java线程的状态 线程对象在不同的运行时期有不同的状态,状态信息就存在于Thread中的State枚举中,如下所示: public enum State { /** * 至今尚未启动的线程处于这种状 ...

  3. java线程安全— synchronized和volatile

    java线程安全— synchronized和volatile package threadsafe; public class TranditionalThreadSynchronized { pu ...

  4. Java线程演示样例 - 继承Thread类和实现Runnable接口

    进程(Process)和线程(Thread)是程序执行的两个基本单元. Java并发编程很多其它的是和线程相关. 进程 进程是一个独立的执行单元,可将其视为一个程序或应用.然而,一个程序内部同事还包括 ...

  5. JAVA线程及简单同步实现的原理解析

    线程 一.内容简介: 本文主要讲述计算机中有关线程的相关内容,以及JAVA中关于线程的基础知识点,为以后的深入学习做铺垫.如果你已经是高手了,那么这篇文章并不适合你. 二.随笔正文: 1.计算机系统组 ...

  6. Java线程中的同步

    1.对象与锁 每一个Object类及其子类的实例都拥有一个锁.其中,标量类型int,float等不是对象类型,但是标量类型可以通过其包装类来作为锁.单独的成员变量是不能被标明为同步的.锁只能用在使用了 ...

  7. java线程和多线程同步

    java的线程之间资源共享,所以会出现线程同步问题(即,线程安全) 一.线程创建: 方式①:extends java.lang.Thread,重写run(),run方法里是开启线程后要做的事..sta ...

  8. Java 线程控制

    一.线程控制 和线程相关的操作都定义在Thread类中,但在运行时可以获得线程执行环境的信息.比如查看可用的处理器数目(这也行?): public class RunTimeTest { public ...

  9. Java 线程的状态

    Java Thread的运行周期中, 有几种状态, 在 java.lang.Thread.State 中有详细定义和说明: NEW 状态是指线程刚创建, 尚未启动 RUNNABLE 状态是线程正在正常 ...

随机推荐

  1. OceanBase RPC机制简要说明

    http://note.youdao.com/share/?id=d2163a7ba8ec1ec58e64683a961c5121&type=note RPC是OB一个比较重要的各个组件间进行 ...

  2. Windows下gvim的快捷键--“冒号+w+回车”真的很累人

    发现Windows下的gvim支持Ctrl+S保存,Ctrl+A全选,Ctrl+C复制,Ctrl+V粘贴,Ctrl+Z撤销 不过Ctrl+X貌似不太正常(可以剪切,但是不能粘贴) 可能要在安装目录下的 ...

  3. MFC的消息管理

    一般而言,与视图状态和用户输入有关的命令由视图类来处理,与文件操作有关的命令由文档类来处理,与窗口布局有关的命令由主框架类来处理,与程序的运行状态有关的命令由APP类来处理.

  4. HTML5[8]: 图文混排,图片与文字居中对齐

    <img src="image.png"><span>999</span> img { /* ...  */ vertical-align: t ...

  5. 关于win7和xp的屏设置类

    DisplayInfo.h #pragma once enum WinVerDef { WIN_VER_UNKNOWN = -, WIN_VER_95 = , WIN_VER_98, WIN_VER_ ...

  6. 利用__index和__newindex实现默认值表、监控表、只读表

    __index和__newindex实际上相当于是在读写表的时候分别加了一道过滤的逻辑,让读写表的操作可以被监控或说回调,利用这个特性可以实现一些带有特殊功能的表. 带有默认值的表: setdefau ...

  7. TF-IDF 简介

      假设我们手头有大量的文档(或网页), 通常我们会比较关心以下几个问题: 1. 每一个文档的关键词(或主题词)包括哪些? 2. 给定一个(或一组)关键词,与这个(或组)词最相关的文档是哪一个? 3. ...

  8. 通过LINQ表达式树动态构建查询条件

    第一种方法: public static class PredicateExtensions { public static Expression<Func<T, bool>> ...

  9. Ioc原理及常用框架

    1 IoC理论的背景    我们都知道,在采用面向对象方法设计的软件系统中,它的底层实现都是由N个对象组成的,所有的对象通过彼此的合作,最终实现系统的业务逻辑.  图1:软件系统中耦合的对象 如果我们 ...

  10. UICollectionView使用以及与UITableView的区别

    在开始前我们在这先附一段最简单的代码 - (void)viewDidLoad { [super viewDidLoad]; UICollectionViewFlowLayout *layout = [ ...