Java 相当于 Ant 的 Java 任务中的 fork? [英] Java equivalent of fork in Java task of Ant?

查看:30
本文介绍了Java 相当于 Ant 的 Java 任务中的 fork?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ant Java 任务提供fork参数,根据定义如果启用会触发另一个 VM 中的类执行".由于我们正在处理大量数据,因此设置此参数可以避免耗尽 Java 堆空间.
我们希望能够通过 Java 类来做同样的事情.实现 fork 提供的功能的最佳方式是什么?

Ant Java task provides fork parameter, which, by definition "if enabled triggers the class execution in another VM". As we are dealing with a large amount of data, setting this parameter saved us from running out of Java heap space.
We want to be able to do the same through a Java class. What would be the best way to achieve the functionality as provided by fork?

推荐答案

执行另一个 java 进程.例如,通过使用 ProcessBuilder 类.

Execute another java process. By using ProcessBuilder class, for example.

http://java.sun.com/javase/6/docs/api/java/lang/ProcessBuilder.html

您可以根据需要运行任意数量的工作进程.让他们拥有一个单独的主类,从该主类中执行任务,并在任务完成后退出.

You can run as many worker processes as you wish. Make them having a separate main class, doing their tasks from that main class, and quiting when their task is completed.

您必须弄清楚它们的类路径以及 java 二进制文件在系统上的位置,但这是可行的.

You'll have to figure out their classpath, and the location of java binary on the system, but that's doable.

我认为您甚至可以在它们完成时通过 Process.waitFor() 收到通知.

I think you can even be notified when they complete via Process.waitFor().

这篇关于Java 相当于 Ant 的 Java 任务中的 fork?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆