}
System.out.println(“End thread: " + Thread.currentThread() + " i–”);
// 等待decThread结束
while (decThread.isAlive());
long duration = System.nanoTime() - curTime;
System.out.println("Result: " + count);
System.out.format(“Duration: %.2fs\n”, duration / 1.0e9);
}
private static class DecThread extends Thread {
@Override
public void run() {
System.out.println(“Start thread: " + Thread.currentThread() + " i–”);
for (int i = 0; i < times; i++) {
count–;
}
System.out.println(“End thread: " + Thread.currentThread() + " i–”);
}
}
}
最后输出的结果是
Start thread: Thread[main,5,main] i++
Start thread: Thread[Thread-0,5,main] i–
End thread: Thread[main,5,main] i–
End thread: Thread[Thread-0,5,main] i–
Result: -460370604
Duration: 67.37s
原因是i++和++i并非原子操作,我们若查看字节码,会发现
void f1() { i++; }
的字节码如下
void f1();
Code:
0: aload_0
1: dup
2: getfield #2; //Field i:I
5: iconst_1
6: iadd
7: putfield #2; //Field i:I
10: return
可见i++执行了多部操作, 从变量i中读取读取i的值 -> 值+1 -> 将+1后的值写回i中,这样在多线程的时候执行情况就类似如下了
Thread1 Thread2
r1 = i; r3 = i;
r2 = r1 + 1; r4 = r3 + 1;
i = r2; i = r4;
这样会造成的问题就是 r1, r3读到的值都是 0, 最后两个线程都将 1 写入 i, 最后 i 等于 1, 但是却进行了两次自增操作
可知加了volatile和没加volatile都无法解决非原子操作的线程同步问题
线程同步问题的解决
Java提供了java.util.concurrent.atomic 包来提供线程安全的基本类型包装类,例子如下
package com.qunar.atomicinteger;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @author zhenwei.liu created on 2013 13-9-2 下午10:18
* @version I d Id Id
*/
public class SafeTest {
private static AtomicInteger count = new AtomicInteger(0);
private static final int times = Integer.MAX_VALUE;
public static void main(String[] args) {
long curTime = System.nanoTime();
Thread decThread = new DecThread();
decThread.start();
// 使用run()来运行结果为0,原因是单线程执行不会有线程安全问题
// new DecThread().run();
System.out.println(“Start thread: " + Thread.currentThread() + " i++”);
for (int i = 0; i < times; i++) {
count.incrementAndGet();
}
// 等待decThread结束
while (decThread.isAlive());
long duration = System.nanoTime() - curTime;
System.out.println("Result: " + count);
System.out.format(“Duration: %.2f\n”, duration / 1.0e9);
}
private static class DecThread extends Thread {
@Override
public void run() {
System.out.println(“Start thread: " + Thread.currentThread() + " i–”);
for (int i = 0; i < times; i++) {
count.decrementAndGet();
}
System.out.println(“End thread: " + Thread.currentThread() + " i–”);
}
}
}
输出
Start thread: Thread[main,5,main] i++
Start thread: Thread[Thread-0,5,main] i–
End thread: Thread[Thread-0,5,main] i–
Result: 0
Duration: 105.15
结论
–
-
volatile解决了线程间共享变量的可见性问题
-
使用volatile会增加性能开销
-
volatile并不能解决线程同步问题
-
解决i++或者++i这样的线程同步问题需要使用synchronized或者AtomicXX系列的包装类,同时也会增加性能开销
推荐文章
原创电子书历时整整一年总结的 Java面试+ Java入门技术学习指南,这是本人这几年及校招的总结,各种异步面试题已经全部进行总结,按照章节复习即可,已经拿到了了大厂提供。
原创思维导图扫码或者微信搜 程序员的技术圈子 回复 面试 领取原创电子书和思维导图。
给大家的福利
零基础入门
对于从来没有接触过网络安全的同学,我们帮你准备了详细的学习成长路线图。可以说是最科学最系统的学习路线,大家跟着这个大的方向学习准没问题。
同时每个成长路线对应的板块都有配套的视频提供:
因篇幅有限,仅展示部分资料
网络安全面试题
绿盟护网行动
还有大家最喜欢的黑客技术
网络安全源码合集+工具包
所有资料共282G,朋友们如果有需要全套《网络安全入门+黑客进阶学习资源包》,可以扫描下方二维码领取(如遇扫码问题,可以在评论区留言领取哦)~
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!