Kotlin,为什么反编译的Java代码将同步锁块转换为Synchronized(var1){} +代码块?

时间:2019-04-18 20:18:33

标签: kotlin locking synchronized

在Kotlin中,有同步块

synchronized(_lock) {
  // code do something
}

这意味着释放锁定,直到完成所有// code fo something

但是在反编译的Java代码中,synchronized(lock)块放在了代码块之外

Object var1 = this._lock;
synchronized(var1){}
// code do something

,这是否意味着该锁将在// code fo something仍在运行的情况下释放得太早?

kotlin代码:

override fun doSomething(): Boolean {
  synchronized(_lock) {

     //......


     lastCompleteAt = Date().time + REQUEST_THROTTLE_TIME

     for ((_, handler) in dataRequestMap) {

        //......
     }
     return true
  }
   }

反编译为Java代码:

public boolean doSomething() {

  Object var1 = this._lock;
  synchronized(var1){}

  boolean var7;
  try {
     //......


     this.lastCompleteAt = (new Date()).getTime() + 5000L;
     Map var3 = (Map)this.dataRequestMap;
     Iterator var4 = var3.entrySet().iterator();

     while(var4.hasNext()) {

        //......
     }

     //......
     var7 = true;
  } finally {
     ;
  }

  return var7;
}

1 个答案:

答案 0 :(得分:2)

我相信@tkausl is correct的原因在于,您的反编译器只是错误地反编译了代码。如果您检查由以下内容生成的字节码:

fun main() {
    val lock = Any()
    synchronized(lock) {
        println("Hello, World!")
    }
}

您会看到:

Compiled from "Main.kt"
public final class MainKt {
  public static final void main();
    Code:
       0: new           #4                  // class java/lang/Object
       3: dup
       4: invokespecial #12                 // Method java/lang/Object."<init>":()V
       7: astore_0
       8: iconst_0
       9: istore_1
      10: iconst_0
      11: istore_2
      12: aload_0
      13: monitorenter
      14: nop
      15: iconst_0
      16: istore_3
      17: ldc           #14                 // String Hello, World!
      19: astore        4
      21: iconst_0
      22: istore        5
      24: getstatic     #20                 // Field java/lang/System.out:Ljava/io/PrintStream;
      27: aload         4
      29: invokevirtual #26                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      32: getstatic     #32                 // Field kotlin/Unit.INSTANCE:Lkotlin/Unit;
      35: astore_2
      36: aload_0
      37: monitorexit
      38: goto          46
      41: astore_2
      42: aload_0
      43: monitorexit
      44: aload_2
      45: athrow
      46: return
    Exception table:
       from    to  target type
          14    36    41   any
          41    42    41   any

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #9                  // Method main:()V
       3: return
}

如果您注意到,在13语句(println("Hello, World"))之前有一个“ monitorenter”命令(29),然后是“ monitorexit”命令(37 )。