字节跳动一面:i++是线程安全的吗?
程序员的成长之路
共 4696字,需浏览 10分钟
·
2020-07-31 19:28
阅读本文大概需要 4 分钟。
# Thread的本地内存
每个Thread都拥有自己的线程存储空间
Thread何时同步本地存储空间的数据到主存是不确定的
# 例子
不用 volatile
使用 volatile
每次修改volatile变量都会同步到主存中
每次读取volatile变量的值都强制从主存读取最新的值(强制JVM不可优化volatile变量,如JVM优化后变量读取会使用cpu缓存而不从主存中读取)
线程 A 中写入 volatile 变量之前可见的变量, 在线程 B 中读取该 volatile 变
量以后, 线程 B 对其他在 A 中的可见变量也可见. 换句话说, 写 volatile 类似于退出同步块, 而读取 volatile 类似于进入同步块
# 注意
public class VolatileTest {
private static volatile int count = 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++;
}
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
void f1() { i++; }
void f1();Code:0: aload_01: dup2: getfield #2; //Field i:I5: iconst_16: iadd7: putfield #2; //Field i:I10: return
Thread1 Thread2r1 = i; r3 = i; r2 = r1 + 1; r4 = r3 + 1;i = r2; i = r4;
# 线程同步问题的解决
package com.qunar.atomicinteger;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @author zhenwei.liu created on 2013 13-9-2 下午10:18
* @version $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 {
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
# 结论
推荐阅读:
Spring Boot+JWT+Shiro+MyBatisPlus 实现 RESTful 快速开发后端脚手架
微信扫描二维码,关注我的公众号
朕已阅
评论