synchronized关键字在方法和代码块的区别

原文链接:https://www.ibm.com/developerworks/library/it-haggar_bytecode/index.html
IBM开发者文档中,提到了synchronized作用于方法或代码块上,编译成class文件后的区别。

java:
public synchronized int top1()
{
return intArr[0];
}
public int top2()
{
synchronized (this) {
return intArr[0];
}
}

class:
Method int top1()
0 aload_0 //Push the object reference(this) at index
//0 of the local variable table.
1 getfield #6 <Field int intArr[]>
//Pop the object reference(this) and push
//the object reference for intArr accessed
//from the constant pool.
4 iconst_0 //Push 0.
5 iaload //Pop the top two values and push the
//value at index 0 of intArr.
6 ireturn //Pop top value and push it on the operand
//stack of the invoking method. Exit.

Method int top2()
0 aload_0 //Push the object reference(this) at index
//0 of the local variable table.
1 astore_2 //Pop the object reference(this) and store
//at index 2 of the local variable table.
2 aload_2 //Push the object reference(this).
3 monitorenter //Pop the object reference(this) and
//acquire the object’s monitor.
4 aload_0 //Beginning of the synchronized block.
//Push the object reference(this) at index
//0 of the local variable table.
5 getfield #6 <Field int intArr[]>
//Pop the object reference(this) and push
//the object reference for intArr accessed
//from the constant pool.
8 iconst_0 //Push 0.
9 iaload //Pop the top two values and push the
//value at index 0 of intArr.
10 istore_1 //Pop the value and store it at index 1 of
//the local variable table.
11 jsr 19 //Push the address of the next opcode(14)
//and jump to location 19.
14 iload_1 //Push the value at index 1 of the local
//variable table.
15 ireturn //Pop top value and push it on the operand
//stack of the invoking method. Exit.
16 aload_2 //End of the synchronized block. Push the
//object reference(this) at index 2 of the
//local variable table.
17 monitorexit //Pop the object reference(this) and exit
//the monitor.
18 athrow //Pop the object reference(this) and throw
//an exception.
19 astore_3 //Pop the return address(14) and store it
//at index 3 of the local variable table.
20 aload_2 //Push the object reference(this) at
//index 2 of the local variable table.
21 monitorexit //Pop the object reference(this) and exit
//the monitor.
22 ret 3 //Return to the location indicated by
//index 3 of the local variable table(14).
Exception table: //If any exception occurs between
from to target type //location 4 (inclusive) and location
4 16 16 any //16 (exclusive) jump to location 16.

由于同步处理和异常处理的完成方式,top2 比前 1 大且速度更慢。请注意,Top1 使用同步方法修改器,该修改器不会生成额外的代码。相比之下,top2 在方法的主体中使用同步语句。

在方法的主体中同步使用可生成监视器输入器和监视exit操作代码的副代码,以及处理异常的其他代码。如果在同步块(监视器)内执行时生成异常,则保证在退出同步块之前释放锁。实施top 1 的效率略高于top 2:这会导致非常小的性能收益。

当存在同步方法修饰符时,如在 top1 中,锁的获取和后续释放不会使用监视器和监视器exit 操作代码完成。相反,当 JVM 调用一种方法时,它会检查ACC_SYNCHRONIZED属性标志。如果存在此标志,执行的线程将获取锁定、调用该方法,然后在方法返回时释放锁。如果从同步方法中抛出异常,则在例外离开方法之前自动释放锁定。

注:如果存在同步方法修饰符,则ACC_SYNCHRONIZED属性标志将包含在方法的method_info结构中。

无论是使用同步方法修改器还是使用同步块,都有大小含义。仅在代码需要同步且您了解其使用所带来的成本时,才使用同步方法。如果需要同步整个方法,我更喜欢方法修改器而不是同步块,以便生成更小、速度稍快的代码。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值