方法签名中具有或不具有同步关键字的方法的相同字节码 [英] Same bytecode for method with or without synchronized keyword in method signature

查看:81
本文介绍了方法签名中具有或不具有同步关键字的方法的相同字节码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于以下两个类,它们具有相同的Java字节码.

For the following 2 classes got the same Java bytecode.

java版本:

java版本"1.8.0_181" Java™SE运行时环境(内部版本1.8.0_181-b13)Java HotSpot(TM)64位服务器VM(内部版本25.181-b13,混合模式)

java version "1.8.0_181" Java(TM) SE Runtime Environment (build 1.8.0_181-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)

javac和javap版本:

javac and javap version:

1.8.0_181

1.8.0_181

我的疑问是

  1. 不应使用 synchronized 关键字的方法具有不同的字节码,正如我们在 synchronized block 中看到的那样,它具有 monitorenter monitorexit ,或者让我们假设我不应该将同步块同步方法然后

  1. shouldn't method with synchronized keyword have different bytecode as we can see in synchronized block has monitorenter and monitorexit, or let us assume I should not mix synchronized block and synchronized method then

JVM如何以不同方式处理这两种方法?

How does JVM handle both methods differently?

public class MySingleton1 {

private MySingleton1() {}

private static MySingleton1 ourInstance;

public static MySingleton1 getInstance() {
    if (ourInstance == null) {
        ourInstance = new MySingleton1();
    }
    return ourInstance;
}
}

public class MySingleton2 {

private MySingleton2() {}

private static MySingleton2 ourInstance;

public static synchronized MySingleton2 getInstance() {
    if (ourInstance == null) {
        ourInstance = new MySingleton2();
    }
    return ourInstance;
}
}

字节码如下:

$javac MySingleton1.java
$javap -c MySingleton1
$javac MySingleton2.java
$javap -c MySingleton2

各个文件的字节码:

Compiled from "MySingleton1.java"
public class MySingleton1 {
  public static MySingleton1 getInstance();
    descriptor: ()LMySingleton1;
    Code:
       0: getstatic     #2                  // Field ourInstance:LMySingleton1;
       3: ifnonnull     16
       6: new           #3                  // class MySingleton1
       9: dup
      10: invokespecial #4                  // Method "<init>":()V
      13: putstatic     #2                  // Field ourInstance:LMySingleton1;
      16: getstatic     #2                  // Field ourInstance:LMySingleton1;
      19: areturn
}

Compiled from "MySingleton2.java"
public class MySingleton2 {
  public static synchronized MySingleton2 getInstance();
    descriptor: ()LMySingleton2;
    Code:
       0: getstatic     #2                  // Field ourInstance:LMySingleton2;
       3: ifnonnull     16
       6: new           #3                  // class MySingleton2
       9: dup
      10: invokespecial #4                  // Method "<init>":()V
      13: putstatic     #2                  // Field ourInstance:LMySingleton2;
      16: getstatic     #2                  // Field ourInstance:LMySingleton2;
      19: areturn
}

我只是想增加我对java w.r.t.的了解.字节码.

I just want to increase my understanding of java w.r.t. bytecode.

如果我的方法错误或问题太琐碎,请让我作为评论.

Let me know as comments if my approach is wrong or if a question is too trivial.

除以下内容外,任何与文档相关的参考文献都非常受欢迎:

Any reference related to documentation is most welcome except the following:

https://en.wikipedia.org/wiki/Java_bytecode

https://en.wikipedia.org/wiki/Java_bytecode_instruction_listings

http://www.cnblogs.com/richaaaard/p/6214929.html

推荐答案

方法上的 synchronized 修饰符被编译到方法标题中的ACC_SYNCHRONIZED标志中.它不影响生成的字节码指令;JVM看到该标志时会隐式添加进入和退出监视器的代码.

The synchronized modifier on the method is compiled into the ACC_SYNCHRONIZED flag in the method header. It does not affect the generated bytecode instructions; the code to enter and exit the monitor is added implicitly by the JVM when it sees that flag.

请参见 JVM规范获取方法标题中标志的完整列表及其含义.

See the JVM specification for the complete list of flags in the method header and their meaning.

这篇关于方法签名中具有或不具有同步关键字的方法的相同字节码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆