当前位置:网站首页>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 .
边栏推荐
- Traversal of binary tree -- restoring binary tree by two different Traversals
- Click the icon is not sensitive how to adjust?
- QT Road (2) -- HelloWorld
- [aaai 2021 timing action nomination generation] detailed interpretation of bsn++ long article
- Recursively process data accumulation
- 6 questions to ask when selecting a digital asset custodian
- Share 𞓜 jointly pre training transformers on unpaired images and text
- NVIDIA SMI has failed because it could't communicate with the NVIDIA driver
- 35. search insertion position
- [go deep into kotlin] - flow advanced
猜你喜欢

C (I) C basic grammar all in one

NVIDIA SMI has failed because it could't communicate with the NVIDIA driver

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

Deep learning distributed training

Technology | image motion drive interpretation of first order motion model

推荐一款免费的内网穿透开源软件,可以在测试本地开发微信公众号使用

IOU series (IOU, giou, Diou, CIO)

Analysis while experiment - a little optimization of memory leakage in kotlin

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

Reverse thinking: making cartoon photos real
随机推荐
6 questions to ask when selecting a digital asset custodian
Target detection - personal understanding of RCNN series
Exploration of kangaroo cloud data stack on spark SQL optimization based on CBO
截取文件扩展名
Maximum number of points on the line ----- hash table solution
【深入kotlin】 - Flow 进阶
Cascade EF gan: local focus progressive facial expression editing
[go deep into kotlin] - get to know flow for the first time
Bert knowledge distillation
jvm调优六:GC日志分析和常量池详解
Zed2 camera calibration -- binocular, IMU, joint calibration
SwiftUI: Navigation all know
Zed2 camera manual
Sealem finance builds Web3 decentralized financial platform infrastructure
Combien de courant le câblage des PCB peut - il supporter?
1.使用阿里云对象OSS(初级)
Section IV: composition and materials of asphalt mixture (1) -- structure composition and classification
How to apply for free idea with official documents
Zed2 running vins-mono preliminary test
In the future, how long will robots or AI have human creativity?