码迷,mamicode.com
首页 > 编程语言 > 详细

指令重排序

时间:2018-05-18 14:12:48      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:int   read   []   sys   adb   slf4j   stat   ati   lag   

@Slf4j
public class SimpleHappenBefore {

    private static int a = 0;

    private static boolean flag = false;

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 1000; i++) {
            ThreadA threadA = new ThreadA();
            ThreadB threadB = new ThreadB();
            threadA.start();
            threadB.start();

            threadA.join();
            threadB.join();
            a = 0;
            flag = false;
        }
    }

    static class ThreadA extends Thread {
        @Override
        public void run() {  //这里的指令重排序
a
= 1; flag = true; } } static class ThreadB extends Thread { @Override public void run() { if (flag) { a = a * 1; } if (a == 0) { System.out.println("ha,a==0"); } } } }

 

指令重排序

标签:int   read   []   sys   adb   slf4j   stat   ati   lag   

原文地址:https://www.cnblogs.com/thiaoqueen/p/9055232.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!