当前位置:网站首页>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 .
边栏推荐
- Section I: classification and classification of urban roads
- 如何让灯具智能化,单火、零火智能开关怎么选!
- Wxparse parsing iframe playing video
- 袋鼠雲數棧基於CBO在Spark SQL優化上的探索
- Get the full link address of the current project request URL
- 27. Remove elements
- Recursively process data accumulation
- 袋鼠云数栈基于CBO在Spark SQL优化上的探索
- [go deep into kotlin] - flow advanced
- [aaai 2021 timing action nomination generation] detailed interpretation of bsn++ long article
猜你喜欢

Stone game -- leetcode practice

Start the project using the locally configured gradle

Tightly coupled laser vision inertial navigation slam system: paper notes_ S2D. 66_ ICRA_ 2021_ LVI-SAM

QT Road (2) -- HelloWorld

BP neural network derivation + Example

Topological sorting

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

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

Paper reproduction: pare

es-ik 安装报错
随机推荐
In the future, how long will robots or AI have human creativity?
【项目篇- 附件佐证材料放什么?(十八种两千字总结)】创新创业竞赛项目计划书、挑战杯创业计划竞赛佐证材料
[project - what are the supporting materials in the annexes? (18 kinds of 2000 word summary)] project plan of innovation and entrepreneurship competition and supporting materials of Challenge Cup entr
The programmers of a large factory after 95 were dissatisfied with the department leaders, and were sentenced for deleting the database and running away
初步了解多任务学习
SwiftUI: Navigation all know
Intercept file extension
Exploration of kangaroo cloud data stack on spark SQL optimization based on CBO
Opencv learning path (2-2) -- Deep parsing namedwindow function
推荐一款免费的内网穿透开源软件,可以在测试本地开发微信公众号使用
How much current can PCB wiring carry
1.使用阿里云对象OSS(初级)
6 questions to ask when selecting a digital asset custodian
2021 iccv paper sharing - occlusion boundary detection
如何让灯具智能化,单火、零火智能开关怎么选!
Oh my Zsh correct installation posture
Topological sorting
自定义View基础之Layout
MySQL nested sorting: first sort and filter the latest data, and then customize the sorting of this list
Take stock of the AI black technologies in the Beijing Winter Olympic Games, and Shenzhen Yancheng Technology