Home  >  Article  >  类库下载  >  Basic knowledge of running java programs

Basic knowledge of running java programs

高洛峰
高洛峰Original
2016-10-29 11:31:041854browse

JVM thread stack to function execution

Every JVM thread will create a private thread stack when it starts. A jvm thread stack is used to store stack frames. The jvm thread stack is very similar to the stack in C language. It is responsible for managing local variables and some operation results, and is also involved in function calls and function returns. The size of the running thread stack in the JVM specification can be fixed or dynamically allocated, or it can be calculated according to certain rules. Different jvm's implementation of the stack will be different. Some may provide developers with ways to control the initial size of the jvm thread stack; for dynamic allocation, they may also provide settings for the jvm's maximum and minimum values.

When calculating the allocation size required by a thread exceeds the fixed value or the set maximum value, jvm will throw a StackOverflowError. For dynamically allocated stacks, if the memory cannot provide enough space to meet the minimum value or the required value, the JVM will throw an OutOfMemoryError

stack frame, which can be understood as a function execution environment, which manages parameters and local variables. , return value, etc.

Each stack frame includes an array that manages local variables (local variables). The number of units in this array can be determined when it is compiled into bytecode. For 32-bit, one unit can store boolean, byte, char, short, int, float, reference, returnAddress; two consecutive units can be used to store long and double. The subscript of the local variable array starts from 0. Generally speaking, the 0 position stores this, followed by the parameters of the function, and then the local variables that appear in the function.

Each stack frame also includes a (LIFO) operation stack data structure (operand stack). Its size can also be determined during compilation. It will be an empty stack when created. Let's take a simple example to describe its common use. For int a+b, first push a into the stack, then push b into the stack, then pop the two values ​​​​at the same time and execute the iadd instruction, and then add them. The result is pushed onto the stack to complete the instruction.

In addition to the above two key structures, each stack frame also has a constant pool (run-time constant pool), exception throwing management and other structures. I won’t go into detail here, you can refer to other information.

Let’s use a simple Demo to illustrate the work of a stack frame. First, let’s look at such a function:

public int comp(float number1, float number2){
        int result ;
        if(number1 < number2)
            result = 1;
        else
            result = 2;
        return result;
    }

The bytecode corresponding to the logic within the function is as follows:

0: fload_1

1: fload_2

2: fcmpg

3: ifge 11

6: iconst_1

7: istore_3

8: goto 13

11: iconst_2

12: istore_3

13: iload_3

14: ireturn

Let’s briefly explain these bytecode instructions:

fload_x: Take the xth variable in the local variable array, type load, push and push it onto the stack;

fcmpg: compare two single-precision floating point numbers. If the two numbers are greater than 1, the result is 0, if they are equal, the result is -1;

ifge: jump instruction;

iconst_x: push constant x onto the stack;

istore_x: pop stack into local variable array The xth one;

iload_x: Read the xth local variable array and push it onto the stack;

ireturn: Return the int type at the end of the function;

If you look closely, you can find that the beginning of i refers to int, and the beginning of f refers to load, load stands for loading, if stands for jump, etc. The operation code definition of bytecode also has a certain meaning. The details can be translated into jvm bytecode related standards. Let’s take a look at how the jvm executes on the stack frame structure, taking the specific call comp(1.02,2.02) as an example:

Basic knowledge of running java programs

Java’s Class

When it comes to bytecode, .class must be indispensable. You might as well take a demo class to see the content of the class in detail. The class is very simple. One of the two functions is say, and the other is the cmp function above.

public class Hello {

    public void say(){
        System.out.println("Hello world!");
    }

    public int comp(float number1, float number2){
        int result ;
        if(number1 < number2)
            result = 1;
        else
            result = 2;
        return result;
    }
}

Use javac -g:none Hello.java to compile this class, and then use javap -c -v Hello.class to parse the compiled class.

Classfile /src/main/java/com/demo/Hello.class
  Last modified 2016-10-28; size 404 bytes
  MD5 checksum 9ac6c800c312d65b568dd2a0718bd2c5public class com.demo.Hello
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:   #1 = Methodref          #6.#14         // java/lang/Object."<init>":()V
   #2 = Fieldref           #15.#16        // java/lang/System.out:Ljava/io/PrintStream;
   #3 = String             #17            // Hello world!
   #4 = Methodref          #18.#19        // java/io/PrintStream.println:(Ljava/lang/String;)V
   #5 = Class              #20            // com/demo/Hello
   #6 = Class              #21            // java/lang/Object
   #7 = Utf8               <init>
   #8 = Utf8               ()V
   #9 = Utf8               Code
  #10 = Utf8               say
  #11 = Utf8               comp
  #12 = Utf8               (FF)I
  #13 = Utf8               StackMapTable
  #14 = NameAndType        #7:#8          // "<init>":()V
  #15 = Class              #22            // java/lang/System
  #16 = NameAndType        #23:#24        // out:Ljava/io/PrintStream;
  #17 = Utf8               Hello world!
  #18 = Class              #25            // java/io/PrintStream
  #19 = NameAndType        #26:#27        // println:(Ljava/lang/String;)V
  #20 = Utf8               com/demo/Hello
  #21 = Utf8               java/lang/Object
  #22 = Utf8               java/lang/System
  #23 = Utf8               out
  #24 = Utf8               Ljava/io/PrintStream;
  #25 = Utf8               java/io/PrintStream
  #26 = Utf8               println
  #27 = Utf8               (Ljava/lang/String;)V{  public com.demo.Hello();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:      stack=1, locals=1, args_size=1
         0: aload_0         1: invokespecial #1                  // Method java/lang/Object."<init>":()V
         4: return

  public void say();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:      stack=2, locals=1, args_size=1
         0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
         3: ldc           #3                  // String Hello world!
         5: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
         8: return

  public int comp(float, float);
    descriptor: (FF)I
    flags: ACC_PUBLIC
    Code:      stack=2, locals=4, args_size=3
         0: fload_1         1: fload_2         2: fcmpg         3: ifge          11
         6: iconst_1         7: istore_3         8: goto          13
        11: iconst_2        12: istore_3        13: iload_3        14: ireturn
      StackMapTable: number_of_entries = 2
        frame_type = 11 /* same */
        frame_type = 252 /* append */
          offset_delta = 1
          locals = [ int ]
}

Explain the new opcodes involved

getstatic:获取镜头变量;
invokevirtual:调用函数;
return:void 函数结束返回;

In the code public int comp(float, float) code you can see the example of the bytecode operation mentioned above. With a perceptual understanding, you can actually roughly see that the class file, in addition to bytecode instructions, also includes constant pool, access flags (public, etc.), and class-related information (attributes, functions, constants, etc.). Because -g:node was used for compilation earlier, other extensions and debugging information can also be included in the class in other modes. The official class file format is detailed as follows:

ClassFile {
    u4             magic;
    u2             minor_version;
    u2             major_version;
    u2             constant_pool_count;
    cp_info        constant_pool[constant_pool_count-1];
    u2             access_flags;
    u2             this_class;
    u2             super_class;
    u2             interfaces_count;
    u2             interfaces[interfaces_count];
    u2             fields_count;
    field_info     fields[fields_count];
    u2             methods_count;
    method_info    methods[methods_count];
    u2             attributes_count;
    attribute_info attributes[attributes_count];
}

magic: It is the very famous 0xCAFEBABE, a logo class file;

minor_version 、major_version :指的是java class 文件的版本,一般说class文件的版本是 XX.xx 其中XX 就是major,xx是minor,比如上面demo中的版本是52.0 代表就是 minor 0,major 51.

constant_pool_count:就是常量池元素个数,cp_info constant_pool[constant_pool_count-1] 就是相关的详细信息了。

access_flags:指的是访问标识例如ACC_PUBLIC、ACC_FINAL、ACC_INTERFACE、ACC_SUPER 写过java的相信看名字应该知道啥意思,ACC是access的缩写。

其他具体的,就不一一介绍了详细可以直接参考官方文档。

动态生成java字节码

当然,你可以直接按照官方的class文件格式来直接写 byte[],然后自定义个 class load 载入编写的byte[]来实现动态生成class。不过,这个要求可能也有点高,必须的非常熟悉class文件格式才能做到。这里demo还是借助 ASM 这个类库来简单演示下,就编写下 上面的Hello 不过里面只实现say的方法。如下:

public class AsmDemo {    public static final String CLASS_NAME = "Hello";    
    public static final AsmDemoLoad load = new AsmDemoLoad();    private static class AsmDemoLoad extends ClassLoader {        public AsmDemoLoad() {            super(AsmDemo.class.getClassLoader());
        }        public Class<?> defineClassForName(String name, byte[] data) {            return this.defineClass(name, data, 0, data.length);
        }
    }    public static byte[] generateSayHello() throws IOException {

        ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS);
        classWriter.visit(V1_7, ACC_PUBLIC + ACC_SUPER, CLASS_NAME, null, getInternalName(Object.class), null);    
        //默认初始化函数
        Method constructorMethod = Method.getMethod("void <init> ()");
        GeneratorAdapter constructor = new GeneratorAdapter(ACC_PUBLIC, constructorMethod, null, null, classWriter);
        constructor.loadThis();        //每个类都要基础Object
        constructor.invokeConstructor(Type.getType(Object.class), constructorMethod);
        constructor.returnValue();
        constructor.endMethod();

        Method mainMethod = Method.getMethod("void say ()");
        GeneratorAdapter main = new GeneratorAdapter(ACC_PUBLIC, mainMethod, null, null, classWriter);
        main.getStatic(Type.getType(System.class), "out", Type.getType(PrintStream.class));
        main.push("Hello world!");
        main.invokeVirtual(Type.getType(PrintStream.class), Method.getMethod("void println (String)"));
        main.returnValue();
        main.endMethod();        return classWriter.toByteArray();
    }    public static void main(String[] args) throws IllegalAccessException, IllegalArgumentException,
            InvocationTargetException, InstantiationException, NoSuchMethodException, SecurityException, IOException {        byte[] code = AsmDemo.generateSayHello();        //反射构建 hello 类,调用hello方法。
        Class<?> hello = load.defineClassForName(CLASS_NAME, code);
        hello.getMethod("say", null).invoke(hello.newInstance(), null);
    }
}

关于动态生成字节码用途,一定场景下是可以提升效率与性能,因为动态生成的类和普通的载入类并无太大区别。手工优化后的字节码执行可能比编译的要优,可以替代反射使用的许多场景 同时避免反射的性能消耗。很著名的一个例子,fastJSON 就是使用内嵌 ASM 框架动态生成字节码类,来进行序列和反序列化工作,是目前公认最快的json字符串解析。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn