二、多线程之Atomic包
一、简介
1、原子操作
我们在使用变量的时候,经常会出现资源竞争的情况,为了保证变量安全,我们就会对对应的方法添加"synchronized"同步锁来达到目的,以保证线程安全。
而原子操作时一种线程安全的操作,在操作执行期间不会穿插任何操作。这意味着,我们不需要使用synchronized等同步串行机制去控制资源的访问。
2、atomic包
JDK在1.5已经开始提供了一些原子类,在:java.util.concurrent.atomic软件包下
这个包下提供了对int、long、array等的原子操作,简单来说,我们可以通过复用这些现有的类来达到线程安全的目的。
JDK文档:http://tool.oschina.net/uploads/apidocs/jdk-zh/java/util/concurrent/atomic/package-frame.html
二、AtomicLong
以下,AtomicLong为例:
java.util.concurrent.atomic.AtomicLong类:http://tool.oschina.net/uploads/apidocs/jdk-zh/java/util/concurrent/atomic/AtomicLong.html
直接继承于Number类
这表明,使用它就像使用Number类一样简单,它就像是一个基本类型的包装类
1)初始化一个值
// 默认初始值为0 public AtomicLong atomicLong = new AtomicLong(); // 也可以赋值 public AtomicLong atomicLong = new AtomicLong(10L);
2)加
// 返回更新的值 atomicLong.addAndGet(1L) // 返回更新前的值 atomicLong.getAndAdd(1L)
3)自增
// 自增,返回更新的值 atomicLong.incrementAndGet(); // 自增,返回旧的值 atomicLong.getAndIncrement()
4)自减
// 自减,返回更新的值 atomicLong.decrementAndGet(); // 自减,返回旧的值 atomicLong.getAndDecrement();
5)设置值
// 设置值 atomicLong.set(10L); // 最后设定为给定值 atomicLong.lazySet(10L); // 设置值,返回旧值 atomicLong.getAndSet(2L); // 如果当前值 == 预期的值 那么设置为给定值 atomicLong.compareAndSet(1L, 2L); // 如果当前值 == 预期的值 那么设置为给定值 atomicLong.weakCompareAndSet(1L, 2L);
6)类型转换
// 返回double atomicLong.doubleValue(); // 返回float atomicLong.floatValue(); // 返回int atomicLong.intValue(); // 返回long atomicLong.longValue(); // 转换为字符串 atomicLong.toString();
7)获取当前值
atomicLong.get()
我们看到以上罗列的一些操作,把很多复合操作都变成了原子操作。还有一些常用的原子类,如:AtomicInterger、AtomicReference等类同
二、多线程之Atomic包的更多相关文章
- Java多线程之Atomic:原子变量与原子类
Atomic简介 Atomic包是java.util.concurrent下的另一个专门为线程安全设计的Java包,包含多个原子操作类这个包里面提供了一组原子变量类. 其基本的特性就是在多线程 ...
- java多线程之Concurrent包
1.在新增的Concurrent包中,BlockingQueue很好的解决了多线程中,如何高效安全“传输”数据的问题. 2.通过这些高效并且线程安全的队列类,为我们快速搭建高质量的多线程程序带来极大的 ...
- Java多线程之Runnable与Thread
Java多线程之Thread与Runnable 一.Thread VS Runnable 在java中可有两种方式实现多线程,一种是继承Thread类,一种是实现Runnable接口:Thread类和 ...
- python 线程之 threading(三)
python 线程之 threading(一)http://www.cnblogs.com/someoneHan/p/6204640.html python 线程之 threading(二)http: ...
- Java多线程之ConcurrentSkipListMap深入分析(转)
Java多线程之ConcurrentSkipListMap深入分析 一.前言 concurrentHashMap与ConcurrentSkipListMap性能测试 在4线程1.6万数据的条件下, ...
- 【C#】线程之Parallel
在一些常见的编程情形中,使用任务也许能提升性能.为了简化变成,静态类System.Threading.Tasks.Parallel封装了这些常见的情形,它内部使用Task对象. Parallel.Fo ...
- 多线程爬坑之路-J.U.C.atomic包下的AtomicInteger,AtomicLong等类的源码解析
Atomic原子类:为基本类型的封装类Boolean,Integer,Long,对象引用等提供原子操作. 一.Atomic包下的所有类如下表: 类摘要 AtomicBoolean 可以用原子方式更新的 ...
- 并发包学习之-atomic包
一,模拟并发代码: 线程不安全的代码 //并发模拟代码 public class CountExample { //请求总数 public static int clientTotal = 5000; ...
- JAVA多线程之volatile 与 synchronized 的比较
一,volatile关键字的可见性 要想理解volatile关键字,得先了解下JAVA的内存模型,Java内存模型的抽象示意图如下: 从图中可以看出: ①每个线程都有一个自己的本地内存空间--线程栈空 ...
随机推荐
- 通过API方式查看Azure Sign-ins记录
经确认,目前Sign-ins功能在中国区还没有开通.也没有相关的预计开通时间.您可以通过如下链接随时关注China Azure的最新公告:https://www.azure.cn/zh-cn/what ...
- 解决pymysql.err.InternalError: (1366, "Incorrect string value: '\\xF0\\x9F\\x8C\\xB8' for column 'headline' at row 1")
解决pymysql.err.InternalError: (1366, "Incorrect string value: '\\xF0\\x9F\\x8C\\xB8' for column ...
- CUDA安装
1.CUDA是什么? CUDA(Compute Unified Device Architecture),显卡厂商NVidia推出的运算平台. 随着显卡的发展,GPU越来越强大,而且GPU为显示图像做 ...
- python学习,day3:函数式编程,*arge,**kwargs
对于不固定长度的参数,需要使用*arge,**kwargs来调用,区别是*arge是转换为元组,而kwargs转化为字典 # coding=utf-8 # Author: RyAn Bi def te ...
- secureCRT scripts as vbs
gdb multithread debug. lsusb.py # $language = "VBScript" # $interface = "1.0" Su ...
- 【实战】Axis2后台Getshell
实战遇到的情况---任意文件读取,读取/conf/axis2.xml内容,读取用户名和密码登录后台 当然弱口令也是屡试不爽的. 操作起来 1.上传cat.aar(链接:https://pan.baid ...
- 查看APK包签名的方法。
1.查看 keystore $ keytool -list -keystore debug.keystore 结果: Keystore type: JKS Keystore provider: SUN ...
- mono for android之文件系统与应用程序首选项(转)
Aside from persistent files, your application might need to store cache data in a file. To do that, ...
- 菜单根据菜单ID向下递归
第一步:我们根据这个类定义数据库,并插入菜单数据 DROP TABLE IF EXISTS `jrbac_menu`; CREATE TABLE `jrbac_menu` ( `id` ) NOT N ...
- WCF系列教程之WCF服务协定
本文参考自:http://www.cnblogs.com/wangweimutou/p/4422883.html,纯属读书笔记,加深记忆 一.服务协定简介: 1.WCF所有的服务协定层里面的服务接口, ...