1 package org.apache.maven.lifecycle.internal;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.codehaus.plexus.component.annotations.Component;
23 import org.codehaus.plexus.component.annotations.Requirement;
24 import org.codehaus.plexus.logging.Logger;
25
26 import java.util.concurrent.ExecutorService;
27 import java.util.concurrent.Executors;
28
29
30
31
32 @Component( role = ThreadConfigurationService.class )
33 public class ThreadConfigurationService
34 {
35 @Requirement
36 private Logger logger;
37
38 private final int cpuCores;
39
40
41 @SuppressWarnings( { "UnusedDeclaration" } )
42 public ThreadConfigurationService()
43 {
44 cpuCores = Runtime.getRuntime().availableProcessors();
45 }
46
47 public ThreadConfigurationService( Logger logger, int cpuCores )
48 {
49 this.logger = logger;
50 this.cpuCores = cpuCores;
51 }
52
53
54 public ExecutorService getExecutorService( String threadCountConfiguration, boolean perCoreThreadCount,
55 int largestBuildListSize )
56 {
57 Integer threadCount = getThreadCount( threadCountConfiguration, perCoreThreadCount, largestBuildListSize );
58 return getExecutorService( threadCount );
59
60
61 }
62
63 private ExecutorService getExecutorService( Integer threadCount )
64 {
65 if ( threadCount == null )
66 {
67 logger.info( "Building with unlimited threads" );
68 return Executors.newCachedThreadPool();
69 }
70
71 logger.info( "Building with " + threadCount + " threads" );
72 return Executors.newFixedThreadPool( threadCount );
73 }
74
75
76
77
78
79
80
81
82
83
84 Integer getThreadCount( String threadCountConfiguration, boolean perCoreThreadCount, int largestBuildListSize )
85 {
86
87 float threadCount = Math.min( cpuCores, largestBuildListSize );
88 if ( threadCountConfiguration != null )
89 {
90 try
91 {
92 threadCount = Float.parseFloat( threadCountConfiguration );
93 }
94 catch ( NumberFormatException e )
95 {
96 logger.warn(
97 "Couldn't parse thread count, will default to " + threadCount + ": " + threadCountConfiguration );
98 }
99 }
100 if ( perCoreThreadCount )
101 {
102 threadCount = threadCount * cpuCores;
103 }
104
105 final int endResult = Math.round( threadCount );
106 if ( logger.isDebugEnabled() )
107 {
108 logger.debug( "Thread pool size: " + endResult );
109 }
110 return endResult;
111 }
112 }