简体   繁体   English

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

[英]Using multiple cores/processors when compiling Java

I use a desktop with eight cores to build a Java application using Ant (through a javac target). 我使用具有八个内核的桌面来使用Ant(通过javac目标)构建Java应用程序。 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? 我知道我可以并行运行几个Ant任务,但我不认为这可以应用于单个编译目标,或者它可以吗?

I don't know of any way to do tell ant itself to make effective use of multiple cores. 我不知道有什么方法可以告诉ant本身有效使用多个内核。 But you can tell ant to use the Eclipse Compiler , which has support for multithreaded compilation built-in. 但是你可以告诉ant使用Eclipse Compiler ,它支持内置的多线程编译

As long as the javac you are calling doesn't use all the cores it doesn't really matter what you say in Ant. 只要您调用的javac不使用所有内核,您在Ant中所说的内容并不重要。 You can use the compiler attribute to define which java compiler should be used for the task. 您可以使用compiler属性来定义应该为任务使用哪个java编译器。

If you have several build targets you can use fork=yes to 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

The documentation seems 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. 任何试图并行运行大型Ant任务序列的人,例如javadoc和javac,都隐含地承担了识别和修复所有并发错误的任务。

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

You can use Buck Build to increase your build speed and utilize multiple cores. 您可以使用Buck Build来提高构建速度并利用多个核心。

In a nutshell: 简而言之:

Buck is a build system developed and used by Facebook. Buck是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. Buck可以并行构建独立的工件,以利用机器上的多个内核。 Further, it reduces incremental build times by keeping track of unchanged modules so that the minimal set of modules is rebuilt. 此外,它通过跟踪未更改的模块来减少增量构建时间,从而重建最小的模块集。

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. 然后可以将完全重建留给构建服务器。

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. 我认为它可能没有多大帮助,因为javac可以将内存中的所有文件拉出来,如果它必须通过多个进程执行此操作,那么它只需要加倍努力。 However if you want to compile two fairly separate pieces of Java code, then you can just do: 但是,如果要编译两个相当独立的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个文件的依赖关系大多不同,那么它可能会节省时间,如果依赖关系重叠,那么它可能不会节省太多时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM