AtomicBoolean源码解析
2017-10-31 11:32
337 查看
再看AsyncTask时使用到了此类,就顺手研究了一下
AtomicBoolean 比较简单的一个类,但是好处多多. 使用起来比较方便先介绍一下他的好处
它主要的作用是用来作判断使用的,这里很多人就有疑问为什么不使用boolean 变量下面就是它的好处
1.它支持线程同步
2.它支持跨进程
使用起来也比较简单 一个指定默认值一个不指定默认值
改变状态
获取状态
其他类似类 AtomicBoolean,AtomicInteger,AtomicIntegerArray,AtomicLong,AtomicLongArray
等等
AtomicBoolean 比较简单的一个类,但是好处多多. 使用起来比较方便先介绍一下他的好处
它主要的作用是用来作判断使用的,这里很多人就有疑问为什么不使用boolean 变量下面就是它的好处
1.它支持线程同步
private volatile int value;
2.它支持跨进程
public class AtomicBoolean implements java.io.Serializable
使用起来也比较简单 一个指定默认值一个不指定默认值
/** * Creates a new {@code AtomicBoolean} with the given initial value. * * @param initialValue the initial value */ public AtomicBoolean(boolean initialValue) { value = initialValue ? 1 : 0; } /** * Creates a new {@code AtomicBoolean} with initial value {@code false}. */ public AtomicBoolean() { }
改变状态
/** * Unconditionally sets to the given value. * * @param newValue the new value */ public final void set(boolean newValue) { value = newValue ? 1 : 0; }
获取状态
/** * Returns the current value. * * @return the current value */ public final boolean get() { return value != 0; }
其他类似类 AtomicBoolean,AtomicInteger,AtomicIntegerArray,AtomicLong,AtomicLongArray
等等
相关文章推荐
- django源码解析之 BooleanField (二)
- django源码解析之 BooleanField (三)
- JDK 源码解析 —— AtomicInteger
- Jdk1.6 JUC源码解析(1)-atomic-AtomicXXX
- lesson8:AtomicInteger源码解析及性能分析
- AtomicInteger源码解析
- AtomicInteger源码解析
- [Java多线程]-J.U.C.atomic包下的AtomicInteger,AtomicLong等类的源码解析
- AtomicInteger源码解析
- Java-Classloader-loadeClass(String,boolean)、findClass(String)类加载源码解析
- 读AtomicBoolean源码之浅析
- JDK之Boolean源码解析
- JUC源码分析1-原子变量-AtomicInteger/AtomicBoolean/AtomicLong/AtomicReference
- 源码解读:java 解析字符串为boolean四种实现方法的细节
- ReactiveSwift源码解析(十一) Atomic的代码实现以及其中的Defer延迟、Posix互斥锁、递归锁
- ReactiveSwift源码解析(十一) Atomic的代码实现以及其中的Defer延迟、Posix互斥锁、递归锁
- AtomicReference源码解析和使用
- AtomicInteger源码解析
- HashMap源码解析
- 5.2 dubbo-compiler源码解析