当前位置:网站首页>JUC atomic array
JUC atomic array
2022-06-13 09:05:00 【Q z1997】
JUC An array of atoms
AtomicIntegerArray
- AtomicLongArray
- AtomicReferenceArray
/** 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);
}
Unsafe array
demo(
()->new int[10],
(array)->array.length,
(array, index) -> array[index]++,
array-> System.out.println(Arrays.toString(array))
);
Safe array
demo(
()-> new AtomicIntegerArray(10),
(array) -> array.length(),
(array, index) -> array.getAndIncrement(index),
array -> System.out.println(array)
);
边栏推荐
- Qvector shallow copy performance test
- You don't know the usage of stringstream
- 13.inline,const,mutable,this,static
- Sonar scan ignores the specified file
- [network security] webshell empowerment of new thinking of SQL injection
- Library management system based on wechat applet Rar (thesis + source code)
- Web page H5 wechat sharing
- 20211115 any n-order square matrix is similar to triangular matrix (upper triangle or lower triangle)
- 15. copy constructor
- Collection of garbled code problems in idea development environment
猜你喜欢
The Jenkins console does not output custom shell execution logs
20211020 academician all drive system
Basic use of cesium, including loading images, terrain, models, vector data, etc
Installation of sonarqube code quality management platform (to be continued)
Diversified tables through TL table row consolidation
教程篇(5.0) 04. Fortint云服务和脚本 * FortiEDR * Fortinet 网络安全专家 NSE 5
Message Oriented Middleware
Mttr/mttf/mtbf diagram
transforms. ColorJitter(0.3, 0, 0, 0)
How to become a white hat hacker? I suggest you start from these stages
随机推荐
Redis fuzzy query batch deletion
20211020 academician all drive system
Pytorch model tuning - only some layers of the pre training model are loaded
Tutorial (5.0) 02 Management * fortiedr * Fortinet network security expert NSE 5
Detailed explanation of C language callback function
An error CV2 is reported when the picture is converted to grayscale cvtColor(img, cv2.COLOR_BGR2GRAY)
MySQL startup error: innodb: operating system error number 13 in a file operation
What are the bank financial products? How long is the liquidation period?
[QNX hypervisor 2.2 user manual] 4.5 building a guest
银行理财产品有哪些?清算期是多长?
Collection of various books
""? "& in URL Role of "" sign
Screenshot of cesium implementation scenario
How to become a white hat hacker? I suggest you start from these stages
Summary of the first retrospective meeting
20211108 能观能控,可稳可测
Web page H5 wechat sharing
Simulink的Variant Model和Variant Subsystem用法
Number of parameters of pytorch statistical model
Animation through svg