我使用以下代码作为CompilerPhase
类的一部分。该方法由编译器的main方法调用(和基准测试)。
ParallelCompilerPhase:
private Consumer<ICompilationUnit> apply;
// ...
@Override
public void apply(Collection<ICompilationUnit> units)
{
this.count = units.size();
for (ICompilationUnit unit : units)
{
new Thread()
{
@Override
public void run()
{
ParallelCompilerPhase.this.apply.accept(unit);
ParallelCompilerPhase.this.count--;
}
}.start();
}
long now = System.currentTimeMillis();
while (this.count > 0)
{
long l = System.currentTimeMillis() - now;
if (l >= 1000L)
{
DyvilCompiler.logger.warning(this.name + " is taking too long! " + l + " ms");
try
{
Thread.sleep(1000L);
}
catch (InterruptedException ex)
{
ex.printStackTrace();
}
}
}
}
CompilerPhase:
private Consumer<Collection<ICompilationUnit>> apply;
//...
@Override
public void apply(Collection<ICompilationUnit> units)
{
this.apply.accept(units);
}
使用旧的实现(CompilerPhase
),整个过程(11个不同阶段)花费40-60毫秒来编译1个编译单元。但是,新实现(ParallelCompilerPhase
)会增加2000毫秒的开销。阶段TOKENIZE
,PARSE
,RESOLVE_TYPES
,RESOLVE
,CHECK
,PRINT
和COMPILE
使用ParallelCompilerPhase
。
以下是编译器的输出:
[2015-03-04 23:16:49] [INFO]: Loaded 2 Libraries (235.7 ms, 117.9 ms/L, 8.48 L/s)
[2015-03-04 23:16:49] [INFO]: Compiling 'src/test' to 'dbin'
[2015-03-04 23:16:49] [INFO]: Applying 8 States: [TOKENIZE, PARSE, RESOLVE_TYPES, RESOLVE, CHECK, PRINT, COMPILE, TEST]
[2015-03-04 23:16:49] [INFO]: Compiling 2 Packages, 2 Files (1 Compilation Unit)
[2015-03-04 23:16:49] [INFO]: Applying State TOKENIZE
[2015-03-04 23:16:49] [INFO]: Finished State TOKENIZE (2.4 ms, 2.4 ms/CU, 423.19 CU/s)
[2015-03-04 23:16:49] [INFO]: Applying State PARSE
[2015-03-04 23:16:50] [WARNING]: PARSE is taking too long! 1000 ms
[2015-03-04 23:16:51] [INFO]: Finished State PARSE (2005.1 ms, 2005.1 ms/CU, 0.50 CU/s)
[2015-03-04 23:16:51] [INFO]: Applying State RESOLVE_TYPES
[2015-03-04 23:16:51] [INFO]: Finished State RESOLVE_TYPES (17.1 ms, 17.1 ms/CU, 58.35 CU/s)
[2015-03-04 23:16:51] [INFO]: Applying State RESOLVE
[2015-03-04 23:16:51] [INFO]: Finished State RESOLVE (24.0 ms, 24.0 ms/CU, 41.70 CU/s)
[2015-03-04 23:16:51] [INFO]: Applying State CHECK
[2015-03-04 23:16:51] [INFO]: Finished State CHECK (0.5 ms, 0.5 ms/CU, 1838.24 CU/s)
[2015-03-04 23:16:51] [INFO]: Applying State PRINT
[2015-03-04 23:16:51] [INFO]: src/test/dyvil/test/Main.dyvil:
// ...
[2015-03-04 23:16:51] [INFO]: Finished State PRINT (42.3 ms, 42.3 ms/CU, 23.61 CU/s)
[2015-03-04 23:16:51] [INFO]: Applying State COMPILE
[2015-03-04 23:16:51] [INFO]: Finished State COMPILE (5.2 ms, 5.2 ms/CU, 192.64 CU/s)
[2015-03-04 23:16:51] [INFO]: Applying State TEST
[2015-03-04 23:16:51] [INFO]: Finished State TEST (46.0 ms, 46.0 ms/CU, 21.72 CU/s)
[2015-03-04 23:16:51] [INFO]: Compilation finished (2148.6 ms, 2148.6 ms/CU, 0.47 CU/s)
// ...
[2015-03-04 23:16:51] [INFO]: Test completed without Errors (1 ms)
但是,如果我将ParallelCompilerPhase
的实现更改为:
@Override
public void apply(Collection<ICompilationUnit> units)
{
for (ICompilationUnit unit : units)
{
this.apply.accept(unit);
}
}
编译器的输出如下所示:
[2015-03-04 23:21:36] [INFO]: Dyvil Compiler 1.0.0 for Dyvil 1.0.0
[2015-03-04 23:21:36] [INFO]: Loaded 2 Libraries (245.6 ms, 122.8 ms/L, 8.14 L/s)
[2015-03-04 23:21:36] [INFO]: Compiling 'src/test' to 'dbin'
[2015-03-04 23:21:36] [INFO]: Applying 8 States: [TOKENIZE, PARSE, RESOLVE_TYPES, RESOLVE, CHECK, PRINT, COMPILE, TEST]
[2015-03-04 23:21:36] [INFO]: Compiling 2 Packages, 2 Files (1 Compilation Unit)
[2015-03-04 23:21:36] [INFO]: Applying State TOKENIZE
[2015-03-04 23:21:36] [INFO]: Finished State TOKENIZE (0.6 ms, 0.6 ms/CU, 1721.17 CU/s)
[2015-03-04 23:21:36] [INFO]: Applying State PARSE
[2015-03-04 23:21:36] [INFO]: Finished State PARSE (20.6 ms, 20.6 ms/CU, 48.59 CU/s)
[2015-03-04 23:21:36] [INFO]: Applying State RESOLVE_TYPES
[2015-03-04 23:21:36] [INFO]: Finished State RESOLVE_TYPES (8.5 ms, 8.5 ms/CU, 117.34 CU/s)
[2015-03-04 23:21:36] [INFO]: Applying State RESOLVE
[2015-03-04 23:21:36] [INFO]: Finished State RESOLVE (15.9 ms, 15.9 ms/CU, 63.07 CU/s)
[2015-03-04 23:21:36] [INFO]: Applying State CHECK
[2015-03-04 23:21:36] [INFO]: Finished State CHECK (0.2 ms, 0.2 ms/CU, 4587.16 CU/s)
[2015-03-04 23:21:36] [INFO]: Applying State PRINT
[2015-03-04 23:21:36] [INFO]: src/test/dyvil/test/Main.dyvil:
// ...
[2015-03-04 23:21:36] [INFO]: Finished State PRINT (2.1 ms, 2.1 ms/CU, 479.39 CU/s)
[2015-03-04 23:21:36] [INFO]: Applying State COMPILE
[2015-03-04 23:21:36] [INFO]: Finished State COMPILE (4.0 ms, 4.0 ms/CU, 251.76 CU/s)
[2015-03-04 23:21:36] [INFO]: Applying State TEST
[2015-03-04 23:21:36] [INFO]: Finished State TEST (0.6 ms, 0.6 ms/CU, 1686.34 CU/s)
[2015-03-04 23:21:36] [INFO]: Compilation finished (57.5 ms, 57.5 ms/CU, 17.40 CU/s)
// ...
[2015-03-04 23:21:36] [INFO]: Test completed without Errors (2 ms)
造成2000毫秒开销的原因是什么?
作为可能的解决方案,将用{/ 1>替换ParallelCompilerPhase
的实现
units.parallelStream().forEach(this.apply);
做我最初想用Thread方法做的事情?
答案 0 :(得分:3)
在ParallelCompilerPhase中,发生以下情况
this.count
和时间,因此新线程不运行问题是繁忙的循环 尝试:
@Override
public void apply(Collection<ICompilationUnit> units)
{
this.count = units.size();
for (ICompilationUnit unit : units)
{
new Thread()
{
@Override
public void run()
{
ParallelCompilerPhase.this.apply.accept(unit);
ParallelCompilerPhase.this.count--;
}
}.start();
}
long now = System.currentTimeMillis();
while (this.count > 0)
{
long l = System.currentTimeMillis() - now;
if (l >= 1000L)
{
DyvilCompiler.logger.warning(this.name + " is taking too long! " + l + " ms");
try
{
Thread.sleep(1000L);
}
catch (InterruptedException ex)
{
ex.printStackTrace();
}
}
try
{
Thread.sleep(10L);
}
catch (InterruptedException ex)
{
ex.printStackTrace();
}
}
}
但是,等待线程的最佳方法是使用Thread.join()
,正如@rici所建议的那样,因为这将是“官方方式”,并且不会造成任何处理器的浪费时间。通过上面的解决方案,主线程在工人完成后等待额外的时间长达10ms,join()
主线程将在工人完成后立即唤醒。
答案 1 :(得分:0)
您正在为每个工作单元开始一个新线程。这是一个可怕的想法。对于任何计算限制的任务(不会花费大部分时间阻塞等待IO的任务),没有任何理由拥有比拥有CPU内核更多的线程。超过这个阈值所做的就是在线程之间切换浪费时间(并浪费大量时间旋转和拆除线程,这并不便宜)。无论有多少线程在运行,你的处理器实际上不能同时处理比处理器资源更多的事情。
相反,您应该考虑使用Executor为您管理thread pool,并让工作线程从队列中弹出工作单元并执行它们。