当前位置:网站首页>Multithreading tutorial (XXVI) field updater and atomic accumulator
Multithreading tutorial (XXVI) field updater and atomic accumulator
2022-06-11 05:29:00 【Have you become a great God today】
Multithreading tutorial ( hexacosa- ) Field updater 、 Atomic accumulator
One 、 Field updater
Common field updaters :
AtomicReferenceFieldUpdater // Domain Field
AtomicIntegerFieldUpdater
AtomicLongFieldUpdater
Using field Updater , You can target a domain of the object (Field) Perform atomic operations , Can only cooperate with volatile Modified field usage , Otherwise, there will be an exception
Exception in thread "main" java.lang.IllegalArgumentException: Must be volatile type
Example :
public class Test5 {
private volatile int field;
public static void main(String[] args) {
AtomicIntegerFieldUpdater fieldUpdater =
AtomicIntegerFieldUpdater.newUpdater(Test5.class, "field");
Test5 test5 = new Test5();
fieldUpdater.compareAndSet(test5, 0, 10);
// Modification successful field = 10
System.out.println(test5.field);
// Modification successful field = 20
fieldUpdater.compareAndSet(test5, 10, 20);
System.out.println(test5.field);
// Modification failed field = 20
fieldUpdater.compareAndSet(test5, 10, 30);
System.out.println(test5.field);
}
}
Output
10
20
20
Two 、 Atomic accumulator
The atomic accumulator mainly solves the problem of multithreading i++ Thread insecurity
1. Accumulator performance comparison
private static <T> void demo(Supplier<T> adderSupplier, Consumer<T> action) {
T adder = adderSupplier.get();
long start = System.nanoTime();
List<Thread> ts = new ArrayList<>();
// 4 Threads , Everyone adds up 50 ten thousand
for (int i = 0; i < 40; i++) {
ts.add(new Thread(() -> {
for (int j = 0; j < 500000; j++) {
action.accept(adder);
}
}));
}
ts.forEach(t -> t.start());
ts.forEach(t -> {
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
long end = System.nanoTime();
System.out.println(adder + " cost:" + (end - start)/1000_000);
}
Compare AtomicLong And LongAdder
for (int i = 0; i < 5; i++) {
demo(() -> new LongAdder(), adder -> adder.increment());
}
for (int i = 0; i < 5; i++) {
demo(() -> new AtomicLong(), adder -> adder.getAndIncrement());
}
Output
1000000 cost:43
1000000 cost:9
1000000 cost:7
1000000 cost:7
1000000 cost:7
1000000 cost:31
1000000 cost:27
1000000 cost:28
1000000 cost:24
1000000 cost:22
The reason for the performance improvement is simple , It's when there's competition , Set multiple accumulation units ,Therad-0 Add up Cell[0], and Thread-1 Add up Cell[1]… Finally, summarize the results . In this way, they operate differently when accumulating Cell Variable , Therefore, it is reduced CAS Retry fail , To improve performance .
LongAdder The design is very clever , If you are interested, you can take a look at his code and design principles , I won't introduce you here . The most important pseudo sharing will be introduced later .
边栏推荐
- ROS compilation error: could not find a package configuration file provided by "XXX“
- PCB走线到底能承载多大电流
- Topological sorting
- Basics of customized view
- JVM tuning 6: GC log analysis and constant pool explanation
- 微信小程序,购买商品属性自动换行,固定div个数,超出部分自动换行
- mysql字符串转数组,合并结果集,转成数组
- Preliminary understanding of DFS and BFS
- Get the third-party interface
- Combien de courant le câblage des PCB peut - il supporter?
猜你喜欢
![[NIPS2021]MLP-Mixer: An all-MLP Architecture for Vision](/img/89/66c30ea8d7969fef76785da1627ce5.jpg)
[NIPS2021]MLP-Mixer: An all-MLP Architecture for Vision

Share | guide language image pre training to achieve unified visual language understanding and generation

Zed2 camera manual

Overview of self attention acceleration methods: Issa, CCNET, cgnl, linformer

微信小程序text内置组件换行符不换行的原因-wxs处理换行符,正则加段首空格

Cross modal retrieval | visual representation learning

Paper reproduction: expressive body capture

Recommend a free intranet penetration open source software that can be used in the local wechat official account under test

String sorting times --- bubble sorting deformation

NVIDIA SMI has failed because it could't communicate with the NVIDIA driver
随机推荐
Opencv learning path (2-5) -- Deep parsing imwrite function
Conversion relationship between coordinate systems (ECEF, LLA, ENU)
智能门锁为什么会这么火,米家和智汀的智能门锁怎么样呢?
袋鼠云数栈基于CBO在Spark SQL优化上的探索
Create cool collectionviewcell conversion animation
如何让灯具智能化,单火、零火智能开关怎么选!
GAMES101作业7-Path Tracing实现过程&代码详细解读
Handle double quotation mark escape in JSON string
Tightly coupled laser vision inertial navigation slam system: paper notes_ S2D. 66_ ICRA_ 2021_ LVI-SAM
The programmers of a large factory after 95 were dissatisfied with the department leaders, and were sentenced for deleting the database and running away
1. use alicloud object OSS (basic)
Recommend a free intranet penetration open source software that can be used in the local wechat official account under test
Stone game -- leetcode practice
点击图标不灵敏咋整?
Use acme SH automatically apply for a free SSL certificate
1.使用阿里云对象OSS(初级)
Customize the layout of view Foundation
About custom comparison methods of classes and custom methods of sort functions
[aaai 2021 timing action nomination generation] detailed interpretation of bsn++ long article
WinForm (II) advanced WinForm and use of complex controls