编译 Java 时使用多个内核/处理器

发布于 2024-09-19 04:01:33 字数 127 浏览 5 评论 0原文

我使用具有八核的桌面来使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

差↓一点笑了 2024-09-26 04:01:33

我不知道有什么方法可以告诉 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.

白衬杉格子梦 2024-09-26 04:01:33

只要您调用的 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

傲鸠 2024-09-26 04:01:33

文档似乎表明它不太可能与 javac 一起正常工作

任何尝试并行运行大型 Ant 任务序列(例如同时运行 javadoc 和 javac)的人都隐含地承担着识别和修复其运行任务的所有并发错误的任务。

因此,虽然此任务有用途,但应将其视为一项高级任务,应在某些批处理或测试情况下使用,而不是加快多路 CPU 上构建时间的简单技巧。

The documentation seems to indicate that it's unlikely to work correctly with javac.

Anyone trying to run large Ant task sequences in parallel, such as javadoc and javac at the same time, is implicitly taking on the task of identifying and fixing all concurrency bugs the tasks that they run.

Accordingly, while this task has uses, it should be considered an advanced task which should be used in certain batch-processing or testing situations, rather than an easy trick to speed up build times on a multiway CPU.

百变从容 2024-09-26 04:01:33

您可以使用 Buck Build 来提高构建速度并利用多个内核。

简而言之:

Buck 是 Facebook 开发和使用的构建系统。它鼓励
创建由代码和组成的小型可重用模块
资源丰富,并且支持多种平台上的多种语言。

Buck 并行构建独立的工件以利用多个
您机器上的核心。此外,它还减少了增量构建时间
跟踪未更改的模块,以便获得最小的模块集
已重建。

You can use Buck Build to increase your build speed and utilize multiple cores.

In a nutshell:

Buck is a build system developed and used by Facebook. It encourages
the creation of small, reusable modules consisting of code and
resources, and supports a variety of languages on many platforms.

Buck builds independent artifacts in parallel to take advantage of multiple
cores on your machine. Further, it reduces incremental build times by
keeping track of unchanged modules so that the minimal set of modules
is rebuilt.

明月夜 2024-09-26 04:01:33

据我所知还没有。 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.

白云悠悠 2024-09-26 04:01:33

我认为这可能没有多大帮助,因为 javac 可以将所有文件提取到内存中,如果它必须对多个进程执行此操作,那么工作量就会加倍。但是,如果您想编译两个相当独立的 Java 代码片段,那么您可以这样做:

#!/usr/bin/env bash

javac file1.java &
javac file2.java &
javac file3.java &

wait;

如果这 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:

#!/usr/bin/env bash

javac file1.java &
javac file2.java &
javac file3.java &

wait;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文