当前位置:网站首页>Multithreading tutorial (XXV) atomic array
Multithreading tutorial (XXV) atomic array
2022-06-11 05:29:00 【Have you become a great God today】
Multithreading tutorial ( twenty-five ) An array of atoms
There are three kinds of commonly used atomic arrays :
AtomicIntegerArray
AtomicLongArray
AtomicReferenceArray
AtomicIntegerArray Array to store integers ;
AtomicLongArray The array stores long integers ;
AtomicReferenceArray The array stores reference types ;
give an example :
/** Parameters 1, Provide arrays 、 It can be a thread unsafe array or a thread safe array Parameters 2, Method to get the length of the array Parameters 3, Self increasing method , Comes back array, index Parameters 4, How to print an array */
// supplier Provider Out of thin air ()-> result
// function function One parameter, one result ( Parameters )-> result , BiFunction ( Parameters 1, Parameters 2)-> result
// consumer consumer A parameter has no result ( Parameters )->void, BiConsumer ( Parameters 1, Parameters 2)->
private static <T> void demo(
Supplier<T> arraySupplier,
Function<T, Integer> lengthFun,
BiConsumer<T, Integer> putConsumer,
Consumer<T> printConsumer ) {
List<Thread> ts = new ArrayList<>();
T array = arraySupplier.get();
int length = lengthFun.apply(array);
for (int i = 0; i < length; i++) {
// Each thread is grouped in pairs 10000 operations
ts.add(new Thread(() -> {
for (int j = 0; j < 10000; j++) {
putConsumer.accept(array, j%length);
}
}));
}
ts.forEach(t -> t.start()); // Start all threads
ts.forEach(t -> {
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}); // When all threads end
printConsumer.accept(array);
}
The above code uses functional programming , You can see the introduction of functional programming Blog
1. Pass a non thread safe array into the method
demo(
()->new int[10],
(array)->array.length,
(array, index) -> array[index]++,
array-> System.out.println(Arrays.toString(array))
);
Output :
[9870, 9862, 9774, 9697, 9683, 9678, 9679, 9668, 9680, 9698]
2. Pass a thread safe array into a function
demo(
()-> new AtomicIntegerArray(10),
(array) -> array.length(),
(array, index) -> array.getAndIncrement(index),
array -> System.out.println(array)
);
result
[10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000]
reference :
边栏推荐
- Conversion relationship between coordinate systems (ECEF, LLA, ENU)
- [go deep into kotlin] - get to know flow for the first time
- 27、移除元素
- Cascade EF gan: local focus progressive facial expression editing
- Combien de courant le câblage des PCB peut - il supporter?
- Target detection - personal understanding of RCNN series
- Sealem finance builds Web3 decentralized financial platform infrastructure
- Using keras to build the basic model yingtailing flower
- Inventory | ICLR 2022 migration learning, visual transformer article summary
- 【项目篇- 附件佐证材料放什么?(十八种两千字总结)】创新创业竞赛项目计划书、挑战杯创业计划竞赛佐证材料
猜你喜欢
随机推荐
wxParse解析iframe播放视频
22. Generate parentheses
微信小程序text内置组件换行符不换行的原因-wxs处理换行符,正则加段首空格
智能门锁为什么会这么火,米家和智汀的智能门锁怎么样呢?
Zed2 running vins-mono preliminary test
Solving graph problems with union search set
Deep extension technology: intelligent OCR recognition technology based on deep learning has great potential
Section II: structural composition characteristics of asphalt pavement (1) structural composition
Course design summary
Poverty has nothing to do with suffering
Share 𞓜 jointly pre training transformers on unpaired images and text
Yolov5 training personal data set summary
如何让灯具智能化,单火、零火智能开关怎么选!
初步了解多任务学习
2021 iccv paper sharing - occlusion boundary detection
String sorting times --- bubble sorting deformation
Conversion relationship between coordinate systems (ECEF, LLA, ENU)
使用acme.sh自动申请免费SSL证书
1. use alicloud object OSS (basic)
Common methods of tool class objectutil



![[NIPS2021]MLP-Mixer: An all-MLP Architecture for Vision](/img/89/66c30ea8d7969fef76785da1627ce5.jpg)





