synzhronized原理3
2016-07-08 08:28
411 查看
1、java中的每个对象都可作为锁,有三种表现形式:
对于普通方法,锁的是当前this对象。
对于静态方法,锁的是class对象
对于方法块,锁的是synchronized指定的对象。
2、JVM基于Monitor对象来实现昂发的同步和代码块同步,但是实现细节不一样。代码块同步是使用monitorenter和monitorexit指令实现,而方法同步使用的是另外一种方式实现的。
3、Java's monitor supports two kinds of thread synchronization: mutual exclusion and cooperation.
4、synchronized锁不一定是FIFO的。
it might make sense to have ten FIFO queues, one for each priority a thread can have inside the Java virtual machine. The virtual machine could then choose the thread that has been waiting the longest in the highest priority queue that contains any waiting threads. 、
5、每个对象和每个Class对象都有与其对应的监视器对象
In the Java virtual machine, every object and class is logically associated with a monitor. For objects, the associated monitor protects the object's instance variables. For classes, the monitor protects the class's class variables. If an object has no instance variables, or a class has no class variables, the associated monitor protects no data.
6、抛异常的时候锁会自动退出
7、方法块和方法锁的字节码区别见http://www.artima.com/insidejvm/ed2/threadsynchP.html
方法块有monitorenter和monitorexit指令
方法没有
1、If you compare these bytecodes with the ones shown earlier for
3、Another difference between the two
8、
对于普通方法,锁的是当前this对象。
对于静态方法,锁的是class对象
对于方法块,锁的是synchronized指定的对象。
2、JVM基于Monitor对象来实现昂发的同步和代码块同步,但是实现细节不一样。代码块同步是使用monitorenter和monitorexit指令实现,而方法同步使用的是另外一种方式实现的。
3、Java's monitor supports two kinds of thread synchronization: mutual exclusion and cooperation.
4、synchronized锁不一定是FIFO的。
it might make sense to have ten FIFO queues, one for each priority a thread can have inside the Java virtual machine. The virtual machine could then choose the thread that has been waiting the longest in the highest priority queue that contains any waiting threads. 、
5、每个对象和每个Class对象都有与其对应的监视器对象
In the Java virtual machine, every object and class is logically associated with a monitor. For objects, the associated monitor protects the object's instance variables. For classes, the monitor protects the class's class variables. If an object has no instance variables, or a class has no class variables, the associated monitor protects no data.
6、抛异常的时候锁会自动退出
7、方法块和方法锁的字节码区别见http://www.artima.com/insidejvm/ed2/threadsynchP.html
方法块有monitorenter和monitorexit指令
方法没有
1、If you compare these bytecodes with the ones shown earlier for
KitchenSync's
reverseOrder()method, you will see that these bytecodes are in effect those of
KitchenSyncwith the support for entering and exiting the monitor removed. 2、Instructions at offset 0 through 56 of
HeatSync's bytecodes correspond to instructions at offset 4 through 68 of
KitchenSync's bytecodes. Because
HeatSync's
reverseOrder()method doesn't need a local variable slot to store the reference to the locked object, the local variable positions used by each method are different. The function of the instructions themselves, however, match up exactly.
3、Another difference between the two
reverseOrder()methods is that the compiler doesn't create an exception table for
HeatSync's
reverseOrder()method. In
HeatSync's case, an exception table isn't necessary. When this method is invoked, the Java virtual machine automatically acquires the lock on the
thisobject. If this method completes abruptly, just as if it completes normally, the virtual machine will release the lock on the
thisobject automatically.
8、
相关文章推荐
- APP开发流程实例讲解-儒释道网络电台八天开发全程-界面美化并进一步优化排错
- 【bzoj3530】【sdoi2014】【数数】【AC自动机+数位dp】
- hibernate多表关联的简单写法
- 彩票小例子
- 16.14
- 函数
- 我们是怎么做Code Review的
- eval简单用法
- linux shell 中的sleep命令
- 数组打乱小例子
- binding.BindingException: Invalid bound statement(not found)
- Java中Collection集合
- Eclipse调试问题 adb connection Error:远程主机强迫关闭了一个现有的连接
- tab切换
- binding.BindingException: Invalid bound statement(not found)
- 数组
- 23种设计模式
- 仪表电机抖动问题的一种程序bug可能性
- oracle递归查询
- Ambari学习笔记