And*_*s_D 13
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
称之为
java -cp <classpath for all applications!> Launcher com.example.App1 com.example.App2
Run Code Online (Sandbox Code Playgroud)
应该在同一个VM内并行执行应用程序App1和App2.
| 归档时间: |
|
| 查看次数: |
13940 次 |
| 最近记录: |