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

    你可能感兴趣的文章
    Nginx 反向代理 MinIO 及 ruoyi-vue-pro 配置 MinIO 详解
    查看>>
    nginx 反向代理 转发请求时,有时好有时没反应,产生原因及解决
    查看>>
    Nginx 反向代理解决跨域问题
    查看>>
    Nginx 反向代理配置去除前缀
    查看>>
    nginx 后端获取真实ip
    查看>>
    Nginx 学习总结(16)—— 动静分离、压缩、缓存、黑白名单、性能等内容温习
    查看>>
    Nginx 学习总结(17)—— 8 个免费开源 Nginx 管理系统,轻松管理 Nginx 站点配置
    查看>>
    Nginx 常用配置清单
    查看>>
    nginx 常用配置记录
    查看>>
    Nginx 我们必须知道的那些事
    查看>>
    Nginx 的 proxy_pass 使用简介
    查看>>
    Nginx 的配置文件中的 keepalive 介绍
    查看>>
    Nginx 负载均衡与权重配置解析
    查看>>
    Nginx 负载均衡详解
    查看>>
    nginx 配置 单页面应用的解决方案
    查看>>
    nginx 配置https(一)—— 自签名证书
    查看>>
    nginx 配置~~~本身就是一个静态资源的服务器
    查看>>
    Nginx 配置解析:从基础到高级应用指南
    查看>>
    nginx+Tomcat性能监控
    查看>>
    Nginx下配置codeigniter框架方法
    查看>>