博客
关于我
夜光带你走进 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/

    你可能感兴趣的文章
    Objective-C实现interpolation search插值搜索算法(附完整源码)
    查看>>
    Objective-C实现Interpolation search插值查找算法(附完整源码)
    查看>>
    Objective-C实现intersection交集算法(附完整源码)
    查看>>
    Objective-C实现intro sort内省排序算法(附完整源码)
    查看>>
    Objective-C实现inverse matrix逆矩阵算法(附完整源码)
    查看>>
    Objective-C实现inversions倒置算法(附完整源码)
    查看>>
    Objective-C实现isalpha函数功能(附完整源码)
    查看>>
    Objective-C实现islower函数功能(附完整源码)
    查看>>
    Objective-C实现isPowerOfTwo算法(附完整源码)
    查看>>
    Objective-C实现isupper函数功能(附完整源码)
    查看>>
    Objective-C实现ItemCF算法(附完整源码)
    查看>>
    Objective-C实现ItemCF算法(附完整源码)
    查看>>
    Objective-C实现iterating through submasks遍历子掩码算法(附完整源码)
    查看>>
    Objective-C实现iterative merge sort迭代归并排序算法(附完整源码)
    查看>>
    Objective-C实现jaccard similarity相似度无平方因子数算法(附完整源码)
    查看>>
    Objective-C实现Julia集算法(附完整源码)
    查看>>
    Objective-C实现jump search跳转搜索算法(附完整源码)
    查看>>
    Objective-C实现jumpSearch跳转搜索算法(附完整源码)
    查看>>
    Objective-C实现k nearest neighbours k最近邻分类算法(附完整源码)
    查看>>
    Objective-C实现k-means clustering均值聚类算法(附完整源码)
    查看>>