线程探讨
发布日期:2021-07-20 20:54:06 浏览次数:36 分类:技术文章

本文共 1656 字,大约阅读时间需要 5 分钟。

public static void main(String[] args) throws InterruptedException {		//ruleStatisMap 是volatile/static修饰的concurrentHashmap并且已经初始化 		/**		 * 情况一		 */		//无线程安全问题		RuleUtils.ruleStatisMap.put("123", new AtomicLong(0));		AtomicLong count = RuleUtils.ruleStatisMap.get("123");		AtomicLong count1 = RuleUtils.ruleStatisMap.get("123");		count.incrementAndGet();		count.incrementAndGet();		System.out.println(count1);				/**		 * 情况二		 */		AtomicLong count2 = RuleUtils.ruleStatisMap.get("123");		AtomicLong count3 = RuleUtils.ruleStatisMap.get("123");		count2 = new AtomicLong(0);		System.out.println(count3);//输出为空		/**		 * 输出10000		 */		for (int i = 0; i < 10000; i++) {			new Thread(new Runnable() {				@Override				public void run() {					if (null == RuleUtils.ruleStatisMap.get("123")) {						synchronized (RuleProcessServiceImpl.class) {							if (null == RuleUtils.ruleStatisMap.get("123")) {								System.err.println("新建");								RuleUtils.ruleStatisMap.put("123", new AtomicLong(0));							}						}					}					RuleUtils.ruleStatisMap.get("123").incrementAndGet();				}			}).start();		}				/**		 * 输出9995		 */		for (int i = 0; i < 10000; i++) {			new Thread(new Runnable() {				@Override				public void run() {					AtomicLong long1 = RuleUtils.ruleStatisMap.get("123");					if (null == long1) {						synchronized (RuleProcessServiceImpl.class) {							if (null == long1) {								long1 = new AtomicLong(0);								System.err.println("新建");								RuleUtils.ruleStatisMap.put("123", long1);							}						}					}					long1.incrementAndGet();				}			}).start();		}				Thread.sleep(5000);		System.out.println(RuleUtils.ruleStatisMap.get("123").longValue());	}

 

转载地址:https://blog.csdn.net/liao0801_123/article/details/90634928 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:springBoot项目打成jar包给其他项目引用,读取jar里面的资源文件失败
下一篇:RabbitMQ生产者和消费者代码(单一消费者,消费容器) & 消息确认机制

发表评论

最新留言

不错!
[***.144.177.141]2024年03月10日 07时33分51秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

php 安装zip,php7.4安装zip扩展 2019-04-21
php7 gitbook,php7性能分析扩展工具xhprof 2019-04-21
java wav 切割_java切割音频文件 2019-04-21
java获取服务器编码_使用Java代码获取服务器性能信息及局域网内主机名 2019-04-21
mysql 导出json_如何将MySQL数据库导出到JSON? 2019-04-21
嵌入式Linux咨询公司,Technical support and consulting 2019-04-21
linux 离线安装中文字库,centos7 离线安装字体fontconfig 2019-04-21
可以使用鸿蒙系统的55款手机,华为鸿蒙系统首批适配机型即将公布,共有55款产品可升级搭载... 2019-04-21
鸿蒙系统chromeos2.0,【华为鸿蒙系统】鸿蒙OS 2.0 适配计划曝光 2019-04-21
android高德地图设置缩放级别,设置地图中心点/级别 2019-04-21
dv4 安装linux,linux安装中的问题 2019-04-21
gmat阅读.html,GMAT阅读“Ecoefficiency”文章深度分析 2019-04-21
html5 带图片导航,html5 带声音的导航 2019-04-21
point 如何求elbow_机器人学——实践一(Arm Navigation 理论+代码) 2019-04-21
avs3 mkv格式封装_将你的视频无损封装成MP4,非转码哦! 2019-04-21
java http服务端_HTTP协议经典面试题整理及答案详解 2019-04-21
mysql 递归查找父节点_数据结构与算法—浅显易懂的二叉排序(查找)树 2019-04-21
body里写注释 postman_使用 Postman 做 API 自动化测试 2019-04-21
python3的配置文件类单例实现_Servlet是单例还多例 2019-04-21
写一个饿汉单例模式的例子_看完这篇单例模式,终于敢和面试官对线了 2019-04-21