public class AtomicTest {
private static volatile int counter = 0;
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
Thread thread = new Thread(() -> {
for (int j = 0; j < 10000; j++) {
//synchronized (AtomicTest.class) {
counter++;
// }
}
});
thread.start();
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//思考counter=?
System.out.println(counter);
}
}
@Slf4j
public class VisibilityTest {
// volatile -> lock addl $0x0,(%rsp)
private boolean flag = true;
// private volatile boolean flag = true;
//private volatile int count;
public synchronized void refresh() {
// 希望结束数据加载工作
flag = false;
System.out.println(Thread.currentThread().getName() + "修改flag:"+flag);
}
public void load() {
System.out.println(Thread.currentThread().getName() + "开始执行.....");
while (flag) {
//TODO 业务逻辑:加载数据
//shortWait(10000);
//synchronized可以保证可见性
//System.out.println("正在加载数据......");
// count++;
//添加一个内存屏障 可以保证可见性
//UnsafeFactory.getUnsafe().storeFence();
// try {
// Thread.sleep(0);
// } catch (InterruptedException e) {
// throw new RuntimeException(e);
// }
//Thread.yield(); //让出cpu使用权
}
System.out.println(Thread.currentThread().getName() + "数据加载完成,跳出循环");
}
public static void main(String[] args) throws InterruptedException {
VisibilityTest test = new VisibilityTest();
// 线程threadA模拟数据加载场景
Thread threadA = new Thread(() -> test.load(), "threadA");
threadA.start();
// 让threadA先执行一会儿后再启动线程B
Thread.sleep(1000);
// 线程threadB通过修改flag控制threadA的执行时间,数据加载可以结束了
Thread threadB = new Thread(() -> test.refresh(), "threadB");
threadB.start();
}
当flag没有volatile修饰时,不可见,执行结果线程A跳不出循环
运行结果:threadA没有跳出循环,也就是说threadB对共享变量flag的更新操作对threadA不可见, 存在可见性问题。
思考:上面例子中为什么多线程对共享变量的操作存在可见性问题?
当flag有volatile修饰时,具有可见性,执行结果线程A可以跳循环
当flag没有volatile修饰时,但是在load()方法内的while()中输出打印语句,如:System.out.println("正在加载数据......")后,,执行结果线程A还是可以跳循环,原因是println()方法内有synchronized (this),具有可见性。
当flag没有volatile修饰时,但是在load()方法内的while()中加上内存屏障后,执行结果线程A也是可以跳循环,具有可见性。
public class UnsafeFactory {
/**
* 获取 Unsafe 对象
* @return
*/
public static Unsafe getUnsafe() {
try {
Field field = Unsafe.class.getDeclaredField("theUnsafe");
field.setAccessible(true);
return (Unsafe) field.get(null);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 获取字段的内存偏移量
* @param unsafe
* @param clazz
* @param fieldName
* @return
*/
public static long getFieldOffset(Unsafe unsafe, Class clazz, String fieldName) {
try {
return unsafe.objectFieldOffset(clazz.getDeclaredField(fieldName));
} catch (NoSuchFieldException e) {
throw new Error(e);
}
}
}
当flag没有volatile修饰时,但是在load()方法内的while()中线程睡眠的方法:如 Thread.sleep(0);后,执行结果线程A也是可以跳循环,sleep(0)方法内部调用了内存屏障,具有可见性!
当sleep中的时间值为0时,相当于调用了Thread.yield(); 让出cpu使用权
1. 通过 volatile 关键字保证可见性
2. 通过 内存屏障保证可见性
3. 通过 synchronized 关键字保证可见性
public class ReOrderTest {
private static int x = 0, y = 0;
private static int a = 0, b = 0;
public static void main(String[] args) throws InterruptedException {
int i=0;
while (true) {
i++;
x = 0;
y = 0;
a = 0;
b = 0;
/**
* x,y的值是多少: 0,1 1,0 1,1 0,0
*/
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
//用于调整两个线程的执行顺序
shortWait(20000);
a = 1; //volatile 写
// 内存屏障StoreLoad lock; addl $0,0(%%rsp)
UnsafeFactory.getUnsafe().storeFence();
x = b; //volatile 读
}
});
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
b = 1;
y = a;
}
});
thread1.start();
thread2.start();
thread1.join();
thread2.join();
System.out.println("第" + i + "次(" + x + "," + y + ")");
if (x==0&&y==0){
break;
}
}
}
public static void shortWait(long interval){
long start = System.nanoTime();
long end;
do{
end = System.nanoTime();
}while(start + interval >= end);
}
}