当前位置:网站首页>Usage of atomicinteger (counter)
Usage of atomicinteger (counter)
2022-07-25 00:03:00 【baidu_ twenty-four million seven hundred and forty-three thousa】
1、 Why use AtomicInteger
- AtomicReference Classes of atomic classes , It is mainly used for efficient program processing in high concurrency environment , To help us simplify synchronization .
- stay Java In language ,++i and i++ Operations are not thread safe , In use , It's inevitable that synchronized keyword . and AtomicInteger Through a thread safe addition and subtraction interface .
AtomicInteger Introduce
AtomicInteger It's an atomic operation Integer class , Add and subtract in a thread safe way .
AtomicInteger Use scenarios
AtomicInteger Provide atomic operations for Integer Use , So it is very suitable for high concurrency .
introduce
import java.util.concurrent.atomic.AtomicInteger;
Get and assign
atomicInteger.get(); // Get current value
atomicInteger.set(0); // set currency
getAndAdd() Methods and AddAndGet Method
AtomicInteger atomicInteger = new AtomicInteger(1);
System.out.println(atomicInteger.get()); --1
System.out.println(atomicInteger.getAndAdd(10)); --1 Get current value , And add 10
System.out.println(atomicInteger.get()); --11
System.out.println(atomicInteger.addAndGet(10)); --21 Get plus 10 After the value of , First plus 10
System.out.println(atomicInteger.get()); --21
getAndDecrement() and DecrementAndGet() Method
AtomicInteger atomicInteger = new AtomicInteger(123);
System.out.println(atomicInteger.get()); --123
System.out.println(atomicInteger.getAndDecrement()); --123 Get the current value and subtract
System.out.println(atomicInteger.get()); --122
System.out.println(atomicInteger.decrementAndGet()); --121 First self subtraction and then obtain subtraction 1 After the value of
System.out.println(atomicInteger.get()); --121边栏推荐
- @Mapkey usage instructions
- With screen and nohup running, there is no need to worry about deep learning code anymore | exiting the terminal will not affect the operation of server program code
- Optaplanner will abandon DRL (drools) scoring method!!!
- [untitled]
- JS ------ Chapter 5 functions and events
- Oracle is not null cannot filter null values
- Be an artistic test / development programmer and slowly change yourself
- 技术操作
- Google Earth engine - the use of the neighborhood tobands function
- EF core :自引用的组织结构树
猜你喜欢
![[leetcode weekly replay] 303rd weekly 20220724](/img/ba/0f16f1f42e4a2593ec0124f23b30d7.png)
[leetcode weekly replay] 303rd weekly 20220724

2022 最 NB 的 JVM 基础到调优笔记, 吃透阿里 P6 小 case

Are you still using system. Currenttimemillis()? Take a look at stopwatch

2022 Henan Mengxin League game 2: Henan University of technology K - Rice

With screen and nohup running, there is no need to worry about deep learning code anymore | exiting the terminal will not affect the operation of server program code

Simple operation K6

Notes of Teacher Li Hongyi's 2020 in-depth learning series 6

First experience of flask

采坑记录:TypeError: 'module' object is not callable

3. Pressure test
随机推荐
Technical operation
How painful is it to write unit tests? Can you do it
云图
[Nuxt 3] (十)运行时配置
QT learning - using database singleton to complete login matching + registration function
EF core :自引用的组织结构树
Excel file processing tool class (based on easyexcel)
Code coverage
Two numbers that appear only once in the array
剖析kubernetes集群内部DNS解析原理
Log4j configuration file
What are the meanings and application scenarios of the three giants of cloud computing: IAAs, PAAS and SaaS?
1. Smoke test
做一个文艺的测试/开发程序员,慢慢改变自己......
Discussion on line segment tree
BGP related knowledge points
ROS机械臂 Movelt 学习笔记3 | kinect360相机(v1)相关配置
Notes of Teacher Li Hongyi's 2020 in-depth learning series 6
[untitled]
Bug summary