一、JMM(java memory model)内存模型

从网上淘来二张图:

java学习:JMM(java memory model)、volatile、synchronized、AtomicXXX理解

上面这张图说的是,在多核CPU的系统中,每个核CPU自带高速缓存,然后计算机主板上也有一块内存-称为主内(即:内存条)。工作时,CPU的高速缓存中的数据通过一系列手段来保证与主内的数据一致(CacheCoherence),更直白点,高速缓存要从主内中load数据,处理完以后,还要save回主存。

 

java学习:JMM(java memory model)、volatile、synchronized、AtomicXXX理解

上图说的是,java中的各种变量(variable)保存在主存中,然后每个线程自己也有自己的工作内存区(working memory),工作时,线程从主存中把变量副本load到自己的工作内存区,处理完了,再save回主存。

好象很明白,没有什么不好理解的:),

问题来了,如果有二个线程:线程A与线程B, A从主存中读取了变量x(到自己的的工作内存区),正准备处理,这时B修改了主存中的变量x,线程A能看见这种变化吗?(是否需要及时从主存中,加载最新的值),这个问题称为共享变量的可见性。

 

二、volatile、synchronized、AtomicXXX

直接上码:

2.1 版本1

package test.cn.mwee.order.monitor;

/**
 * Created by 菩提树下的杨过 on 2017/6/11.
 */
public class ThreadTest extends Thread {

    private static boolean flag = false;

    public void run() {
        System.out.println("t1:" + Thread.currentThread().getId());
        while (!flag) {

        }
        System.out.println("quit!");
    }

    public static void main(String[] args) throws InterruptedException {
        ThreadTest t1 = new ThreadTest();
        t1.start();
        Thread.sleep(50);
        ThreadTest.flag = true;
        System.out.println("main:" + Thread.currentThread().getId());
    }
}

ThreadTest是一个线程类,里面有一个静态变量flag,然后写了个main方法做测试。

注:在t1启动完成后,主线程中修改了ThreadTest的静态变量值flag,这时t1的run方法里的while循环,其实是看不见主线程对这个值的修改,所以程序始终不能退出,打印不出那一行quit.

 

2.2 版本2

package test.cn.mwee.order.monitor;

/**
 * Created by 菩提树下的杨过 on 2017/6/11.
 */
public class ThreadTest extends Thread {

    private static boolean flag = false;

    public void run() {
        System.out.println("t1:" + Thread.currentThread().getId());
        while (!flag) {
            synchronized (Class.class) {
            }
        }
        System.out.println("quit!");
    }

    public static void main(String[] args) throws InterruptedException {
        ThreadTest t1 = new ThreadTest();
        t1.start();
        Thread.sleep(50);
        ThreadTest.flag = true;
        System.out.println("main:" + Thread.currentThread().getId());
    }
}  

相对版本1,while循环中增加了一个synchronized同步代码块,虽然里面啥代码也没有,但是再次运行,能正常quit了(想下为啥?)

答案:(也是从网上抄来的)

synchronized关键字强制实现一个互斥锁,使得被保护的代码块在同一时间只能有一个线程进入并执行。

相关文章:

  • 2021-06-15
  • 2021-10-14
  • 2022-12-23
  • 2021-04-14
  • 2021-07-16
  • 2021-07-14
猜你喜欢
  • 2021-05-22
  • 2021-06-27
  • 2022-01-24
  • 2021-06-13
  • 2021-06-28
  • 2021-11-29
相关资源
相似解决方案