kotlin,为什么反编译 java 代码将 synchronized 锁块转换为 synchronized(var1){} + 代码块?

kotlin, why decompiled java code translate the synchronized lock block into synchronized(var1){} + code block?

kotlin中有synchronized块

synchronized(_lock) {
  // code do something
}

意思是释放锁直到// code fo something全部完成。

但是在反编译的 java 代码中,同步(锁定)块被放在代码块之外

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

,是不是意味着过早释放了锁,以至于// code fo something可能还是运行?

科特林代码:

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;
}

我相信 您的反编译器只是错误地反编译了代码。如果您检查由以下内容生成的字节码:

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
}

如果你注意到,在println("Hello, World")语句(29)之前有一个"monitorenter"命令(13),后面跟着一个"monitorexit"命令(37).