Count++不是线程安全的 不是原子性的
来源:互联网 发布:淘宝网鞋子帆布鞋 编辑:程序博客网 时间:2024/05/24 07:22
package com.bjsxt.base.sync007;import java.util.concurrent.atomic.AtomicInteger;import com.sun.org.apache.xalan.internal.xsltc.compiler.sym;/** * volatile关键字不具备synchronized关键字的原子性(同步) * @author alienware * */public class VolatileNoAtomic extends Thread{private static volatile int count;//private static AtomicInteger count = new AtomicInteger(0);private static void addCount(){for (int i = 0; i < 100000; i++) {count++ ;//count.incrementAndGet();}System.out.println(count);}public void run(){addCount();}public static void main(String[] args) {VolatileNoAtomic[] arr = new VolatileNoAtomic[100];for (int i = 0; i < 20; i++) {arr[i] = new VolatileNoAtomic();}for (int i = 0; i < 20; i++) {arr[i].start();}}}
如果count++ 满足原子性的话 最后一个值肯定是正确的
现在说的是count++是否具有原子性 所以打印出来的值可能和你预想的不太一样 如果想一样的话 必须是整个方法是加锁才行
0 0
- Count++不是线程安全的 不是原子性的
- DCLP不是线程安全的?
- crypt()不是线程安全的
- 加密算法不是线程安全的
- DefaultControllerFactory不是线程安全的
- ffmpeg不是线程安全的
- contentProvider不是线程安全的
- count++不是原子性操作测试
- Android 的View不是线程安全的.
- 图片缩放UIGraphicsGetImageFromCurrentImageContext不是线程安全的
- 图片缩放UIGraphicsGetImageFromCurrentImageContext不是线程安全的
- STL容器不是线程安全的
- SimpleDateFormat 为什么不是线程安全的
- SimpleDateFormat不是线程安全的类
- hashSet不是线程安全的验证
- android UI操作不是线程安全的
- 为什么SimpleDateFormat不是线程安全的?
- 为什么常规HashMap不是线程安全的?
- linux下生成https的crt和key证书
- centos 7.0 下安装nginx1.11
- SpringMVC框架搭建
- onhashchange
- 图的邻接表存储下的广度优先遍历
- Count++不是线程安全的 不是原子性的
- 快捷键
- 车牌识别之颜色选取
- (二)SQL数据模式、表、索引的建立与删除
- Problem A: 求图形的面积和体积
- Mesh网格编程(一) 流体
- CentOS安装svn服务器
- PAT-1038
- Spring Cloud Eureka: 如何指定Zone