可见性:一个线程对共享变量值的修改,能够及时地被其他线程看到。
共享变量:如果一个变量在多个线程的工作内存中都存在副本,那么这个变量就是这几个线
程的共享变量。
Java内存模型:
线程对共享变量操作的规定:
共享变量可见性实现的原理:
![](https://images2017.cnblogs.com/blog/1000648/201708/1000648-20170821225209683-618137510.png)
可见性的必要条件:
![](https://images2017.cnblogs.com/blog/1000648/201708/1000648-20170821225303074-602730220.png)
可见性的实现方式:(final也可以)
![](https://images2017.cnblogs.com/blog/1000648/201708/1000648-20170821225405527-1759193556.png)
线程执行互斥代码的过程:
![](https://images2017.cnblogs.com/blog/1000648/201708/1000648-20170821225734668-1797425610.png)
重排序的概念:
![](https://images2017.cnblogs.com/blog/1000648/201708/1000648-20170821225823355-772863581.png)
as-if-serial的概念:
![](https://images2017.cnblogs.com/blog/1000648/201708/1000648-20170821225916496-46624497.png)
导致共享变量在线程间不可见的原因:
![](https://images2017.cnblogs.com/blog/1000648/201708/1000648-20170821230004011-1812837367.png)
可见性分析:
![](https://images2017.cnblogs.com/blog/1000648/201708/1000648-20170821230049480-825854798.png)
当一个线程访问object的一个sysnchronized(this)同步代码块时,其他线程对object中所有其他synchronized(this)同步代码块的访问会被阻塞
volatile实现可见性:
![](https://images2017.cnblogs.com/blog/1000648/201708/1000648-20170821230312214-1275801562.png)
volatile不能实现原子性的分析:
![](https://images2017.cnblogs.com/blog/1000648/201708/1000648-20170821230417902-1560880022.png)
保证自增操作原子性的解决方案:
![](https://images2017.cnblogs.com/blog/1000648/201708/1000648-20170821230501386-167208581.png)
ReentrantLock的用法:
![](https://images2017.cnblogs.com/blog/1000648/201708/1000648-20170821230558699-491822990.png)
volatile适用场合:
![](https://images2017.cnblogs.com/blog/1000648/201708/1000648-20170821230651011-1542158753.png)
synchronized与volatile的比较
![](https://images2017.cnblogs.com/blog/1000648/201708/1000648-20170821230731730-1119394534.png)