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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3727493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 03:57:40  来源:igfitidea点击:

Using multiple cores/processors when compiling Java

javaantparallel-builds

提问by Xavier Nodet

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?

我使用具有 8 个内核的桌面来使用 Ant(通过 javac 目标)构建 Java 应用程序。有没有办法通过使用多个线程或进程来加快编译速度?

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?

我知道我可以并行运行多个 Ant 任务,但我认为这不能应用于单个编译目标,或者可以吗?

采纳答案by Joachim Sauer

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 compilationbuilt-in.

我不知道有什么方法可以告诉 ant 本身有效地利用多核。但是您可以告诉 ant 使用 Eclipse Compiler,它支持内置的多线程编译

回答by bosmacs

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

该文档似乎表明它不太可能与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.

任何试图并行运行大型 Ant 任务序列(例如 javadoc 和 javac)的人都隐含地承担了识别和修复它们运行的​​任务的所有并发错误的任务。

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

回答by Matti Lyra

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 compilerattribute to define which java compiler should be used for the task.

只要您调用的 javac 不使用所有内核,您在 Ant 中说什么并不重要。您可以使用该compiler属性来定义该任务应使用哪个 Java 编译器。

If you have several build targets you can use fork=yesto execute the target(s) externally.

如果您有多个构建目标,则可用于fork=yes在外部执行目标。

http://ant.apache.org/manual/Tasks/javac.html#compilervalues

http://ant.apache.org/manual/Tasks/javac.html#compilervalues

回答by Thorbj?rn Ravn Andersen

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.

据我所知没有。Eclipse 编译器已经完成了一些工作来加速使用多核,但它并没有像您希望的那样购买。

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.

问题是,您能否接受增量编译以进行开发,并且只重新编译那些更改过的内容?然后可以将完全重建留给构建服务器。

回答by Lior

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

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

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.

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

Buck 并行构建独立工件以利用您机器上的多个内核。此外,它通过跟踪未更改的模块来减少增量构建时间,以便重新构建最小的模块集。

回答by Alexander Mills

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:

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

#!/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.

如果这 3 个文件的依赖项大多不同,那么它可能会节省时间,如果依赖项重叠,那么它可能不会节省太多时间。