有多少JVM在main内部调用main

1 java performance jvm program-entry-point process

class B {
    public static void main(String[] args) {
        
    }
}
class A {
    public static void main(String[] args) {
        B.main(args);
    }
}
Run Code Online (Sandbox Code Playgroud)

在上面的流程中,我的 init 方法是 A.main,它依次调用 B.main。

  1. 我知道调用 A.main 会产生一个 JVM。在 A.main 中调用 B.main 是否会产生另一个 JVM?或者
  2. B.main是JUST一旦JVM上启动a。主为初始化函数另一种静态方法。

Dun*_*ncG 5

选项2. mains 只是每个类的静态方法,从 A 到 调用时只有一个 JVM 正在运行B.main(args)

您还可以在 JUNIT 测试中使用它来帮助检查命令行启动行为是否符合预期,例如

@Test void coverage() {
   A.main(new String[] { "a","b" }); // or B.main
   // assertions here if there is some output state you could check
}
Run Code Online (Sandbox Code Playgroud)