博客
关于我
夜光带你走进 Java 成神之路(四十一)擅长的领域
阅读量:313 次
发布时间:2019-03-01

本文共 2024 字,大约阅读时间需要 6 分钟。

????Volatile?????

Volatile?Java????????????????????????????????????????????????????????????????????????????????Volatile?????????????????????????

Volatile???

  • ??????Volatile????????????????????????Volatile??????????????????
  • ??????Volatile??????????????????????????? atomic???????????
  • Volatile?????

    • ??????????????Volatile?????????????????????
    • ???????????????????Volatile???????????????????

    Volatile?Synchronized???

    • Volatile???????????????????????????????Volatile???
    • Synchronized??????????????????????????

    Lock????????

    ?Java????????Lock??????????????????synchronized???????????????????????????Lock??????????????????????ReentrantLock?

    ReentrantLock???

    • ???????????????????????
    • fairness???????????????????
    • blocking???????????????????

    Lock?????

    • ??synchronized????????????Lock????????????
    • ?????Lock??????????????????????

    ?????????

    ?????????????????????????????????????????????????????????????????????????????????

    ???

    • ???????????????????????????????????????????????????
    • ??????????????????
    • ???????????????????

    ????

    • ???????????????????????????????????
    • ??????????????????
    • ????????????????

    ????

    ?????Volatile?Lock??????????

    public class Demo {    private volatile int a = 1;    private Lock lock = new ReentrantLock();    public int getA() {        lock.lock();        return a;    }    public void setA(int a) {        lock.lock();        try {            Thread.sleep(2);        } catch (InterruptedException e) {            e.printStackTrace();        }        this.a = a;        lock.unlock();    }    public static void main(String[] args) {        Demo d = new Demo();        d.a = 10;        new Thread(new Runnable() {            @Override            public void run() {                System.out.println(d.a);            }        }).start();        try {            Thread.sleep(100);        } catch (InterruptedException e) {            e.printStackTrace();        }        System.out.println("??????" + d.getA());    }}

    ??

    Volatile?Lock???Java???????????Volatile?????????????????Lock????????????????????????????????????????????

    转载地址:http://dzbo.baihongyu.com/

    你可能感兴趣的文章
    Python 中如何处理异常?
    查看>>
    Python 中如何实现列表的切片?
    查看>>
    Python 中如何实现字典的排序?
    查看>>
    Python 中常用的数据类型及相关操作详解
    查看>>
    python 中文乱码
    查看>>
    Python 中生成器与普通函数的区别
    查看>>
    Python 中的 *tuple 和 **dict 是什么意思?
    查看>>
    Python 中的 filter() 函数:筛选可迭代对象元素
    查看>>
    Python 中的 Pillow 不允许我打开图像(“超出限制“)
    查看>>
    Python 中的 threading 模块和 multiprocessing 模块有何区别?
    查看>>
    Python 中的 threading 模块和 multiprocessing 模块有何区别?
    查看>>
    Python 中的 Turtle 库详解
    查看>>
    Python 中的 __slots__ 属性有什么作用?
    查看>>
    Python 中的... 三点、箭头(->)、/ 分别的作用
    查看>>
    python读取json数据的id_python处理JSON数据
    查看>>
    Python 中的Duck Typing
    查看>>
    Python 中的for in 遍历生成字典、添加判断条件if
    查看>>
    Python 中的Lock与RLock
    查看>>
    Python 中的range(),arange()函数
    查看>>
    Python 中的内存管理机制
    查看>>