同时运行在同一个 JVM 上运行的 Java 程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6264309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:05:41  来源:igfitidea点击:

Simultaneously run java programs run on same JVM?

javajvm

提问by Deepak B

Suppose I run two java programs simultaneously on the same machine. Will the programs run in a single instance of JVM or will they run in two different instances of JVM?

假设我在同一台机器上同时运行两个 java 程序。程序会在单个 JVM 实例中运行还是会在两个不同的 JVM 实例中运行?

采纳答案by Bohemian

If you start each one with the javacommand (from the command line) they will run as totally separate JVMs.

如果您使用java命令(从命令行)启动每一个,它们将作为完全独立的JVM运行。

"Programs" may be started as separate Threadsrunning inside the one JVM.

“程序”可以作为在一个 JVM 内运行的单独线程启动。

回答by Andreas Dolk

javacan just open start one application at a time, but you could write a simple launcherthat takes class names as arguments and executes them in separate threads. A quick outline:

java一次只能打开启动一个应用程序,但您可以编写一个简单的启动器,将类名作为参数并在单独的线程中执行它们。快速概述:

public class Launcher {
  public static void main(String[] args) throws Exception {
    for (int i = 0; i<args.length; i++) {
      final Class clazz = Class.forName(args[i]);
      new Thread(new Runnable() {
        @Override
        public void run() {
           try{
             Method main = clazz.getMethod("main", String[].class);
             main.invoke(null, new Object[]{});
           } catch(Exception e) {
             // improper exception handling - just to keep it simple
           }
        }
      }).start();
    }
  }
}

Calling it like

像这样称呼

  java -cp <classpath for all applications!> Launcher com.example.App1 com.example.App2

should execute the application App1 and App2 inside the same VM and in parallel.

应该在同一个 VM 中并行执行应用程序 App1 和 App2。

回答by Tony the Pony

It depends on the platform and the JVM implementation, but typically they would run in separate VMs.

这取决于平台和 JVM 实现,但通常它们会在单独的 VM 中运行。

回答by Vineet Reynolds

Assuming that you meant processes by the word programs, then yes, starting two processes, will create two different JVMs.

假设您用程序这个词来表示进程,那么是的,启动两个进程将创建两个不同的 JVM。

A JVM process is started using the java application launcher; this ought to provided with an entry-point to your program, which is the main method. You may link to other classes from this entry-point, and from other classes as well. This would continue to occur within the same JVM process, unless you launch another process (to run another program).

使用 java 应用程序启动器启动 JVM 进程;这应该为您的程序提供一个入口点,这是主要方法。您可以从这个入口点链接到其他类,也可以从其他类链接。这将继续发生在同一个 JVM 进程中,除非您启动另一个进程(运行另一个程序)。

回答by Peter Lawrey

Will the programs run in a single instance of JVM or will they run in two different instances of JVM?

程序会在单个 JVM 实例中运行还是会在两个不同的 JVM 实例中运行?

That is up to you. The simplest approach is to use separate JVMs.

那取决于你。最简单的方法是使用单独的 JVM。

回答by Stas Jaro

What you could do is use two separate threads. For exampe

您可以做的是使用两个单独的线程。例如

new Thread() {
  public void run() {
   System.out.println("this is running separately from the main thread!");
  }
}.start();

If you want two separate programs to interact you would need to use sockets

如果您希望两个单独的程序进行交互,则需要使用套接字