java并发计数器问题
来源:互联网 发布:男士风衣品牌 知乎 编辑:程序博客网 时间:2024/05/16 05:46
- package com.tongbanjie.trade.test.base;
- import java.util.concurrent.TimeUnit;
- import java.util.concurrent.atomic.AtomicInteger;
- /**
- * 测试并发累加
- * @author huangqun
- *
- */
- public class TestConcurrentPlusPlus {
- public static int count = 0;
- public volatile static int volatileCount = 0;
- public static int synchronizedCount = 0;
- public static AtomicInteger atomicCount = new AtomicInteger(0);
- public volatile static AtomicInteger volatileAtomicCount = new AtomicInteger(0);
- public static void main(String[] args) {
- final Object lock = new Object();
- for (int i = 0; i < 50000; i++) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- count++;
- volatileCount++;
- synchronized (lock) {
- synchronizedCount++;
- }
- atomicCount.incrementAndGet();
- volatileAtomicCount.incrementAndGet();
- }
- }).start();
- }
- // 休息5秒, 保证线程中的计算完成
- try {
- TimeUnit.SECONDS.sleep(5);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println("线程并发执行对计数器累计5000次,看并发结果!");
- System.out.println("count=" + count);
- System.out.println("volatileCount=" + volatileCount);
- System.out.println("synchronizedCount=" + synchronizedCount);
- System.out.println("atomicCount=" + atomicCount.get());
- System.out.println("volatileAtomicCount=" + volatileAtomicCount.get());
- }
- }
阅读全文
0 0
- java并发计数器问题
- 解决Java多线程并发的计数器问题
- java 并发 (1):原子计数器
- java并发编程(一):计数器
- PHP文件计数器遇到的问题-并发写文件
- JAVA并发编程-障碍器CyclicBarrier,计数器CountDownLatch,信号量Semaphore
- Java并发(三)——同步计数器
- 并发性能计数器模块
- 堆外并发计数器
- java-计数器
- java 的并发问题
- java高并发问题
- java并发DCL问题
- java多线程并发问题
- java并发问题
- java并发问题
- Java并发问题总结
- Java并发面试问题
- 莫比乌斯函数模板
- 一个女程的自我告白
- spring接收json格式的多个对象参数
- Educational Codeforces Round 26
- Window本地安装与启用MongoDB
- java并发计数器问题
- #HDU 2577 How to Type ?
- 动态规划中级教程 377. Combination Sum IV
- WUST 1255 巧克力(线段树的单点区间更新查询)
- 播放wav, PlaySound不太靠谱,有时没有声音,原因未知,改用MCI函数,尚未测试
- 微信-----信息加密与解密流程图的过程
- JAVA中ListIterator和Iterator详解与辨析
- SOAP WebService 短信接口对接
- 【文献阅读】Densely Connected Convolutional Networks-best paper-CVPR-2017