简体   繁体   English

在jar中运行两个类

[英]Running two classes inside jar

I have created a runnable jar with two main classes. 我创建了带有两个主要类的可运行jar。 One is simple class file and other is having thread. 一种是简单的类文件,另一种是具有线程。 I can able to execute both the classes. 我可以执行两个类。 But my question is if we executed the thread class and it starts running. 但是我的问题是我们是否执行了线程类并且它开始运行。 Now we start the execute the normal class often. 现在,我们经常开始执行普通类。 Does this cause over memory usage. 这是否会导致过度使用内存。

Starting both classes separately, starts 2 JVM process for running the code. 分别启动两个类,启动2个JVM进程来运行代码。 Each execution is totally separated and your class 1 can not interact (easily) with your class 2 started in another JVM. 每次执行是完全分开的,您的类1无法与在另一个JVM中启动的类2(轻松地)交互。 You can also start or stop each process regardless of the other. 您也可以启动或停止每个进程,而无需考虑其他进程。 But having 2 JVM running has a cost (especially on memory usage) 但是运行2个JVM会产生成本(尤其是在内存使用方面)

But it can be correct, depending on what you want to do. 但这可能是正确的,具体取决于您要执行的操作。

Otherwise, you can have only one main method which starts both classes in same execution. 否则,您只能使用一个main方法来在同一执行中启动两个类。 Doing that will use only one JVM and processes can be easily communicate, just by calling methods on object instances. 这样做仅使用一个JVM,并且只需在对象实例上调用方法即可轻松地进行通信。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM