Java相当于Ant的Java任务中的fork?
Ant Java任务提供了fork
参数,其中,根据定义“如果启用,则会触发另一个虚拟机中的类执行”。 当我们处理大量数据时,设置此参数可以避免 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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
执行另一个java进程。 例如,通过使用 ProcessBuilder 类。
http://java.sun.com/javase/6 /docs/api/java/lang/ProcessBuilder.html
您可以根据需要运行任意数量的工作进程。 让他们有一个单独的主类,从该主类中执行任务,并在任务完成后退出。
您必须弄清楚它们的类路径以及 java 二进制文件在系统上的位置,但这是可行的。
我认为您甚至可以在它们完成时通过 Process.waitFor() 收到通知。
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.
You'll have to figure out their classpath, and the location of java binary on the system, but that's doable.
I think you can even be notified when they complete via Process.waitFor().
如果您查看
ant
源代码,当fork
为true
时,它只是包装一个Execute
任务,并且最终,被调用的代码是下载并查看 org.apache.tools.ant.taskdefs.Java 和 org.apache.tools.ant.taskdefs 的源代码。 Execute 将为您提供一些很好的指导,帮助您找到以独立于平台的方式运行的可执行文件的位置等。
If you look at the
ant
source code, whenfork
istrue
, then it just wraps anExecute
task and eventually, the code that gets called isDownloading and having a look at the source code for
org.apache.tools.ant.taskdefs.Java
andorg.apache.tools.ant.taskdefs.Execute
will give you some great pointers in finding the location of the executable to run in a platform independent way, etc.我认为你可以直接使用Ant API...Ant可以直接在Java类中使用。 Javadoc 在其二进制发行版中可用。
I think you can directly use the Ant API...Ant can be directly used in a Java class. The Javadoc is available in their binary distribution.