当前位置:网站首页>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
}
}
边栏推荐
猜你喜欢
![[learning notes] DP status and transfer](/img/5e/59c64d2fe08b89fba2d7e1e6de2761.png)
[learning notes] DP status and transfer

Unity3d learning notes 5 - create sub mesh

Summary of development issues

Download address and installation tutorial of vs2015

vulnhub之tomato(西红柿)

Introduction to the implementation principle of rxjs observable filter operator

ES6 standard
![[MySQL special] read lock and write lock](/img/ac/e01c26882cc664ea2e5e731c5a8bab.png)
[MySQL special] read lock and write lock

OpenGL 绘制彩色的三角形

Vulnhub pyexp
随机推荐
(构造笔记)从类、API、框架三个层面学习如何设计可复用软件实体的具体技术
Qt OpenGL 纹理贴图
(database authorization - redis) summary of unauthorized access vulnerabilities in redis
OpenGL draws colored triangles
[MySQL special] read lock and write lock
Dynamically monitor disk i/o with ZABBIX
shardingSphere分库分表<3>
Flutter: self study system
(construction notes) ADT and OOP
(构造笔记)GRASP学习心得
Visual studio 2022 downloading and configuring opencv4.5.5
Simple factory and factory method mode
Qt+vtk+occt reading iges/step model
MySQL uses the method of updating linked tables with update
Introduction to the implementation principle of rxjs observable filter operator
STL Tutorial 9 deep copy and shallow copy of container elements
Redis 笔记 01:入门篇
Wechat applet - basic content
(数据库提权——Redis)Redis未授权访问漏洞总结
temp