编译 Java 时使用多个内核/处理器
我使用具有八核的桌面来使用 Ant(通过 javac 目标)构建 Java 应用程序。有没有一种方法可以通过使用多个线程或进程来加快编译速度?
我知道我可以并行运行多个 Ant 任务,但我认为这不能应用于单个编译目标,是吗?
I use a desktop with eight cores to build a Java application using Ant (through a javac target). Is there a way to speed up the compilation by using more than one thread or process?
I know I can run several Ant tasks in parallel, but I don't think this can be applied to a single compilation target, or does it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我不知道有什么方法可以告诉 ant 本身有效地利用多核。但是 您可以告诉 ant 使用 Eclipse 编译器,它支持 多线程编译内置。
I don't know of any way to do tell ant itself to make effective use of multiple cores. But you can tell ant to use the Eclipse Compiler, which has support for multithreaded compilation built-in.
只要您调用的 javac 不使用所有内核,您在 Ant 中说什么并不重要。您可以使用
compiler
属性来定义应使用哪个 Java 编译器来执行该任务。如果您有多个构建目标,则可以使用
fork=yes
在外部执行目标。http://ant.apache.org/manual/Tasks/javac.html#编译器值
As long as the javac you are calling doesn't use all the cores it doesn't really matter what you say in Ant. You can use the
compiler
attribute to define which java compiler should be used for the task.If you have several build targets you can use
fork=yes
to execute the target(s) externally.http://ant.apache.org/manual/Tasks/javac.html#compilervalues
文档似乎表明它不太可能与
javac 一起正常工作
。The documentation seems to indicate that it's unlikely to work correctly with
javac
.您可以使用 Buck Build 来提高构建速度并利用多个内核。
简而言之:
You can use Buck Build to increase your build speed and utilize multiple cores.
In a nutshell:
据我所知还没有。 Eclipse 编译器做了一些工作来加速使用多核,但它并没有像您希望的那样多。
问题是,您可以接受增量编译进行开发,并且只重新编译那些发生更改的内容吗?然后,完整的重建可以留给构建服务器。
Not as far as I know. The Eclipse compiler has some work done to speed up using multiple cores but it does not buy as much as you probably would like it to.
Question is, can you live with incremental compilation for development, and only recompile those that changed? The full rebuild can then be left to the build server.
我认为这可能没有多大帮助,因为 javac 可以将所有文件提取到内存中,如果它必须对多个进程执行此操作,那么工作量就会加倍。但是,如果您想编译两个相当独立的 Java 代码片段,那么您可以这样做:
如果这 3 个文件具有大部分不同的依赖项,那么它可能会节省时间,如果依赖项重叠,那么它可能不会节省太多时间。
I assume that it might not help much because javac can pull all files in memory and if it has to do this with multiple processes it's just doubling the effort. However if you want to compile two fairly separate pieces of Java code, then you can just do:
if the 3 files have mostly different dependencies, then it might save time, if the dependencies overlap, then it probably doesn't save much time.