《程序员练级攻略》 阅读总结

前言

​ 陈皓叔叔的这个专栏之前就有所耳闻并大致过了一遍,如今重新精读本专栏,认真学习,对整体的学习方向进行把握,并且进行总结。

Java原子类的使用与实现

简介

通常情况下,在Java中,++i这类自增/自减运算符在并发环境中不能保证并发安全。需要通过加锁才能解决并发环境下的原子性问题。Atomic原子类通过CAS方式来解决线程安全问题,CAS是一种无锁算法(乐观锁),乐观锁多用于“读多写少“的环境,避免频繁加锁影响性能;而悲观锁多用于”写多读少“的环境,避免频繁失败和重试影响性能。

Volatile关键字剖析

定义

The Java programming language allows threads to access shared variables (§17.1).As a rule, to ensure that shared variables are consistently and reliably updated, a thread should ensure that it has exclusive use of such variables by obtaining a lock that, conventionally, enforces mutual exclusion for those shared variables.The Java programming language provides a second mechanism, volatile fields,that is more convenient than locking for some purposes.A field may be declared volatile, in which case the Java Memory Model ensures that all threads see a consistent value for the variable (§17.4).

0%