当前位置:网站首页>Use of atomicinteger
Use of atomicinteger
2022-07-03 12:13:00 【zwanying】
AtomicInteger Use
Why java.util.concurrent.atomic.AtomicInteger
++i,–i Not thread safe , Three steps involved ,1、 obtain i value 2、 Carry out operations 3、 Write back the new value . multithreading , The results exceeded expectations .
AtomicInteger What methods are provided
The code comments indicate the methods provided . While using , Familiar with .
import static org.junit.Assert.*;
import java.util.concurrent.atomic.AtomicInteger;
public class Test {
public static void main(String args[]) throws InterruptedException {
final AtomicInteger value = new AtomicInteger(10);
// Get value
assertEquals(value.get(),10);
// Set the value
value.set(5);
assertEquals(value.get(),5);
// Compare and set
// Currently compared with expectations , Not equal, not updated .
assertEquals(value.compareAndSet(3, 2), false);
assertEquals(value.get(), 5);
// Currently compared with expectations , Equal update .
assertEquals(value.compareAndSet(5, 2), true);
assertEquals(value.get(), 2);
//++i
assertEquals(value.incrementAndGet(), 3);
//+n
assertEquals(value.getAndAdd(5),3);
assertEquals(value.get(),8);
//i++
assertEquals(value.getAndIncrement(),8);
assertEquals(value.get(),9);
//+n
assertEquals(value.addAndGet(2),11);
//--i
assertEquals(value.decrementAndGet(),10);
//i--
assertEquals(value.getAndDecrement(),10);
assertEquals(value.get(),9);
//i==n
assertEquals(value.getAndSet(6),9);
assertEquals(value.get(),6);
}
}
verification AtomicInteger Thread safety
import java.util.concurrent.atomic.AtomicInteger;
import static org.junit.Assert.assertEquals;
class Test {
static int ivalue = 10;
public static void main(String args[]) throws InterruptedException {
// Use AtomicInteger, Thread safety
// ordinary ++i, Thread unsafe
final AtomicInteger value = new AtomicInteger(10);
final int threadSize = 50000;
Thread[] t = new Thread[threadSize];
for(int i=0;i<threadSize;i++){
t[i] = new Thread(){
@Override
public void run(){
value.incrementAndGet();
++ivalue;
}
};
}
for(Thread tt :t){
tt.start();
}
for(Thread tt: t){
tt.join();
}
assertEquals(value.get(),threadSize+10);//true
assertEquals(ivalue,threadSize+10);// When there are enough threads ,false
}
}
边栏推荐
- Socket TCP for network communication (I)
- pragma-pack语法与使用
- Talk about the state management mechanism in Flink framework
- 20. Valid brackets
- (construction notes) learn the specific technology of how to design reusable software entities from three levels: class, API and framework
- Pragma pack syntax and usage
- DEJA_VU3D - Cesium功能集 之 054-模拟火箭发射全过程
- Summary of development issues
- QT OpenGL texture map
- Php Export word method (One MHT)
猜你喜欢
Xiaopeng P7 hit the guardrail and the airbag did not pop up. The official responded that the impact strength did not meet the ejection requirements
Shardingsphere sub database and sub table < 3 >
OpenGL 绘制彩色的三角形
【附下载】密码获取工具LaZagne安装及使用
Integer string int mutual conversion
OpenGL draws colored triangles
Qt OpenGL 纹理贴图
Vulnhub geminiinc V2
Qt+vtk+occt reading iges/step model
【mysql官方文档】死锁
随机推荐
shardingSphere分库分表<3>
Wrong arrangement (lottery, email)
Niuniu's team competition
Differences between MySQL Union and union all
Php Export word method (One MHT)
LeetCode 0556.下一个更大元素 III - 4步讲完
Vulnhub pyexp
239. Sliding window maximum
网络通讯之Socket-Tcp(一)
[combinatorics] permutation and combination (example of permutation and combination)
DEJA_VU3D - Cesium功能集 之 054-模拟火箭发射全过程
Visual studio 2022 downloading and configuring opencv4.5.5
CGroup introduction
Master and backup role election strategy in kept
DEJA_ Vu3d - cesium feature set 053 underground mode effect
PHP get the file list and folder list under the folder
Dart: about grpc (I)
SystemVerilog -- OOP -- copy of object
During FTP login, the error "530 login incorrect.login failed" is reported
(构造笔记)ADT与OOP