当前位置:网站首页>【juc学习之路第6天】并发计算器与线程随机因子
【juc学习之路第6天】并发计算器与线程随机因子
2022-06-11 23:51:00 【birdyson】
并发计算器
有些时候不想用原子操作类但还想实现安全的计算,可以使用并发计算器,有两种计算器:
- 累加器:
DoubleAccumulator、LongAccumulator(需要初始化) - 加法器:
DoubleAdder、LongAdder
public class Main {
public static void main(String[] args) throws Exception {
DoubleAccumulator da = new DoubleAccumulator(Double::sum, 12);
da.accumulate(100);
System.out.println(da.get());
LongAdder longAdder = new LongAdder();
longAdder.add(10);
longAdder.add(20);
longAdder.add(30);
System.out.println(longAdder.longValue());
}
}
ThreadLocalRandom
用于解决不同线程之间使用不同的随机因子,相当于此类为每个线程提供了一个随机因子,也就是new Ramdom()
public class Main {
public static void main(String[] args) throws Exception {
for (int i = 0; i < 3; i ++) {
new Thread(() -> {
for (int j = 0; j < 3; j ++) {
System.out.printf("%s: %d\n",
Thread.currentThread().getName(),
ThreadLocalRandom.current().nextInt(100));
}
}).start();
}
}
}
Thread-0: 67
Thread-1: 29
Thread-1: 17
Thread-1: 96
Thread-2: 27
Thread-2: 0
Thread-2: 2
Thread-0: 44
Thread-0: 7
边栏推荐
- sonarqube介紹和安裝步驟
- 【delphi】判断文件的编码方式(ANSI、Unicode、UTF8、UnicodeBIG)
- Test case design method
- 2022 high voltage electrician test question simulation test question bank and online simulation test
- 2022 operation of simulation examination platform for safety officer C certificate
- 2022 618笔记本选购指北
- DOM知識點總結
- Jetpack架构组件学习(3)——Activity Results API使用
- (dp) acwing 902. Minimum editing distance
- Flex flexible layout tutorial and understanding of the main axis cross axis: Grammar
猜你喜欢

Unity3d C # development of wechat games audio / sound playback problem solving process sharing

Jenkins基本配置

图及图的遍历

Lake Shore - supertran VP continuous flow cryogenic thermostat system

Cisco private dynamic routing protocol: EIGRP

Solr之基础讲解入门

(greedy + longest ascending subsequence) acwing 896 Longest ascending subsequence II
![Display product details [project mall]](/img/51/1ead9d9dde9bca6a9acea9667328ac.png)
Display product details [project mall]

2022 high voltage electrician test question simulation test question bank and online simulation test

(counting class +dp) acwing 900 Integer partition
随机推荐
商品热销排行【项目 商城】
Node version control tool NVM
Lake Shore—SuperTran 连续流低温恒温器系统
通过财报读懂企业的23个步骤
HMS core shows the latest open capabilities in mwc2022, helping developers build high-quality applications
Les produits antigéniques entrent dans la famille et les entreprises chinoises de dispositifs médicaux accueillent un nouvel océan bleu
Ar helps brand stores achieve global data growth
2022 high voltage electrician test question simulation test question bank and online simulation test
一文读懂Logstash原理
Postgresql错误码是如何构造的
Acwing's first question solution attracted the first fan!!! Happy~~~
Handwritten simple promise
loading
Wake up wrist - neural network and deep learning (tensorflow application) updating
Mmdetection custom fetch detection result script and image_ demo. Py parsing
Remix localization, loading local contract file, local link Remix
DOM知識點總結
Two ways of using reuqests in RF
HMS core shows the latest open capabilities in mwc2022, helping developers build high-quality applications
Notes on knowledge points of dynamic planning