高并发编程之AtomicLong讲解
一、AtomicLong介绍
AtomicLong是作用是对长整形进行原子操作。
在32位操作系统中,64位的long 和 double 变量由于会被JVM当作两个分离的32位来进行操作,所以不具有原子性。而使用AtomicLong能让long的操作保持原子型。
二、AtomicLong的几个常用方法
①.创建具有初始值 0 的新 AtomicLong。
package chapter3.AtomicLongTest;import java.util.concurrent.atomic.AtomicLong;/*** @author czd*/
public class AtomicLongTest {public static void main(String[] args) {//1、创建具有初始值 0 的新 AtomicLong。AtomicLong atomicLong = new AtomicLong();System.out.println("Value:" + atomicLong.get());}
}
②.创建具有给定初始值的新 AtomicLong。
package chapter3.AtomicLongTest;import java.util.concurrent.atomic.AtomicLong;/*** @author czd*/
public class AtomicLongTest {public static void main(String[] args) {//2、创建具有给定初始值的新 AtomicLong。AtomicLong atomicLong1 = new AtomicLong(10);System.out.println("Value:" + atomicLong1.get());}
}
③.addAndGet()方法:以原子方式将给定值添加到当前值,先加上特定的值,再获取结果
package chapter3.AtomicLongTest;import java.util.concurrent.atomic.AtomicLong;/*** @author czd*/
public class AtomicLongTest {public static void main(String[] args) {//3、以原子方式将给定值添加到当前值,先加上特定的值,再获取结果AtomicLong atomicLong2 = new AtomicLong(3);atomicLong2.addAndGet(5);System.out.println("Value:" + atomicLong2.get());}
}
④.getAndAdd()方法:先获取当前值再加上特定的值
package chapter3.AtomicLongTest;import java.util.concurrent.atomic.AtomicLong;/*** @author czd*/
public class AtomicLongTest {public static void main(String[] args) {//4、先获取当前值再加上特定的值AtomicLong atomicLong5 = new AtomicLong(10);atomicLong5.getAndAdd(5);System.out.println("Value:" + atomicLong5.get());}
}
⑤.compareAndSet()方法:如果当前值 == 预期值,则以原子方式将该值设置为给定的更新值。
package chapter3.AtomicLongTest;import java.util.concurrent.atomic.AtomicLong;/*** @author czd*/
public class AtomicLongTest {public static void main(String[] args) {//5、如果当前值 == 预期值,则以原子方式将该值设置为给定的更新值。AtomicLong atomicLong3 = new AtomicLong(10);atomicLong3.compareAndSet(10,15);System.out.println("Value:" + atomicLong3.get());}
}
⑥.decrementAndGet()方法:以原子方式将当前值减 1,先减去1再获取值
package chapter3.AtomicLongTest;import java.util.concurrent.atomic.AtomicLong;/*** @author czd*/
public class AtomicLongTest {public static void main(String[] args) {//6、 以原子方式将当前值减 1,先减去1再获取值AtomicLong atomicLong4 = new AtomicLong(10);atomicLong4.decrementAndGet();System.out.println("Value:" + atomicLong4.get());}
}
⑦.getAndDecrement()方法:先获取当前值再减1
package chapter3.AtomicLongTest;import java.util.concurrent.atomic.AtomicLong;/*** @author czd*/
public class AtomicLongTest {public static void main(String[] args) {//7、先获取当前值再减1AtomicLong atomicLong6 = new AtomicLong();atomicLong6.getAndDecrement();System.out.println("Value:" + atomicLong6.get());}
}
⑧.getAndIncrement()方法:先获取当前值再加1
package chapter3.AtomicLongTest;import java.util.concurrent.atomic.AtomicLong;/*** @author czd*/
public class AtomicLongTest {public static void main(String[] args) {//8、先获取当前值再加1AtomicLong atomicLong7 = new AtomicLong();atomicLong7.getAndIncrement();System.out.println("Value:" + atomicLong7.get());}
}
⑨.incrementAndGet()方法:先加1再获取当前值
package chapter3.AtomicLongTest;import java.util.concurrent.atomic.AtomicLong;/*** @author czd*/
public class AtomicLongTest {public static void main(String[] args) {//9、先加1再获取当前值AtomicLong atomicLong9 = new AtomicLong();atomicLong9.incrementAndGet();System.out.println("Value:" + atomicLong9.get());}
}
⑩.getAndSet()方法:先获取当前值再设置新的值
package chapter3.AtomicLongTest;import java.util.concurrent.atomic.AtomicLong;/*** @author czd*/
public class AtomicLongTest {public static void main(String[] args) {//10、先获取当前值再设置新的值AtomicLong atomicLong8 = new AtomicLong();atomicLong8.getAndSet(20);System.out.println("Value:" + atomicLong8.get());}
}
高并发编程之AtomicLong讲解相关推荐
- JUC 高并发编程之JUC三大辅助类
JUC 高并发编程之JUC三大辅助类 JUC 中提供了三种常用的辅助类,通过这些辅助类可以很好的解决线程数量过 多时 Lock 锁的频繁操作.这三种辅助类为: CountDownLatch: 减少计数 ...
- 【java 高并发编程之JUC】2w字带你JUC从入门到精通
点击查看脑图目录地址,实时更新 1 什么是 JUC 1.1 JUC 简介 在 Java 中,线程部分是一个重点,本篇文章说的 JUC 也是关于线程的.JUC 就是 java.util .concurr ...
- 【java 高并发编程之JUC】高阶JUC特性总结
1 线程中断机制 1.1 什么是中断? 首先 一个线程不应该由其他线程来强制中断或停止,而是应该由线程自己自行停止.所以,Thread.stop, Thread.suspend, Thread.res ...
- Java高级技术第五章——高并发编程之从synchronized关键字到事务并发的若干问题
前言 前言点击此处查看: http://blog.csdn.net/wang7807564/article/details/79113195 synchronized关键字 通过该关键字的使用,保证可 ...
- 高并发编程之AtomicReference使用场景
Java并发--AtomicReferencen,解决并发时修改多个属性 记录一下工作中,mycat主从延迟,缓存数据有误解决方案 一.AtomicReference介绍 1-AtomicRefere ...
- Java 并发编程之美:并发编程高级篇之一-chat
借用 Java 并发编程实践中的话:编写正确的程序并不容易,而编写正常的并发程序就更难了.相比于顺序执行的情况,多线程的线程安全问题是微妙而且出乎意料的,因为在没有进行适当同步的情况下多线程中各个操作 ...
- Java 并发编程之美:并发编程高级篇之一
借用 Java 并发编程实践中的话:编写正确的程序并不容易,而编写正常的并发程序就更难了.相比于顺序执行的情况,多线程的线程安全问题是微妙而且出乎意料的,因为在没有进行适当同步的情况下多线程中各个操作 ...
- python电路模型编程_14、python开发之路-并发编程之I/O模型
十四.并发编程之I/O模型 http://www.cnblogs.com/linhaifeng/articles/7454717.html 1.模型介绍 1.1 IO种类 (1)* blocking ...
- java并发编程之4——Java锁分解锁分段技术
转载自 java并发编程之4--Java锁分解锁分段技术 并发编程的所有问题,最后都转换成了,"有状态bean"的状态的同步与互斥修改问题.而最后提出的解决"有状态bea ...
最新文章
- 有哪些时间管理的习惯?
- BZOJ.3265.志愿者招募加强版(费用流SPFA)
- freemarker+生成java_Freemarker + xml 实现Java导出word
- Oprofile安装与使用探索
- pandas根据索引删除dataframe列
- python中gensim内没有summarization的问题
- 关于asp.net C# 导出Excel文件 打开Excel文件格式与扩展名指定格式不一致的解决办法...
- Cinema 4D* 中令人惊叹的体积效果
- 编译PBRT-v3源码
- 移动pc多平台运营级家校互动平台系统源码转让
- 小程序在wxml里转数字_微信小程序 之wxml保留小数点后两位数的方法及转化为字符串的方法...
- 阿里云视频点播 和HLS加密解密
- 数据FIFO的读写和信息FIFO的基本使用方法
- 160603、使用pd4ml.jar和ss_css2.jar转pdf的工具类
- 什么是交叉(cross-over)网线?
- laravel 自定义分页样式
- pytorch学习笔记(八):PytTorch可视化工具 visdom
- batocera整合包_OGA 官方固件 + Retroarch + 睡眠模式 ES整合包
- 负责将用户输入的信息转化为计算机,计算机基础复习题参考答案-
- C++实现1.交换两个整形变量的内容.2.不创建临时变量的条件下,交换两个数的内容.3.求是个整数中的最大值.4.将三个数按从大到小输出.5.求两个数的最大公约数.