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 public ThreadConfigurationService()
42 {
43 cpuCores = Runtime.getRuntime().availableProcessors();
44 }
45
46 public ThreadConfigurationService( Logger logger, int cpuCores )
47 {
48 this.logger = logger;
49 this.cpuCores = cpuCores;
50 }
51
52
53 public ExecutorService getExecutorService( String threadCountConfiguration, boolean perCoreThreadCount,
54 int largestBuildListSize )
55 {
56 Integer threadCount = getThreadCount( threadCountConfiguration, perCoreThreadCount, largestBuildListSize );
57 return getExecutorService( threadCount );
58
59
60 }
61
62 private ExecutorService getExecutorService( Integer threadCount )
63 {
64 if ( threadCount == null )
65 {
66 logger.info( "Building with unlimited threads" );
67 return Executors.newCachedThreadPool();
68 }
69
70 logger.info( "Building with " + threadCount + " threads" );
71 return Executors.newFixedThreadPool( threadCount );
72 }
73
74
75
76
77
78
79
80
81
82
83 Integer getThreadCount( String threadCountConfiguration, boolean perCoreThreadCount, int largestBuildListSize )
84 {
85
86 float threadCount = Math.min( cpuCores, largestBuildListSize );
87 if ( threadCountConfiguration != null )
88 {
89 try
90 {
91 threadCount = Float.parseFloat( threadCountConfiguration );
92 }
93 catch ( NumberFormatException e )
94 {
95 logger.warn(
96 "Couldn't parse thread count, will default to " + threadCount + ": " + threadCountConfiguration );
97 }
98 }
99 if ( perCoreThreadCount )
100 {
101 threadCount = threadCount * cpuCores;
102 }
103
104 final int endResult = Math.round( threadCount );
105 if ( logger.isDebugEnabled() )
106 {
107 logger.debug( "Thread pool size: " + endResult );
108 }
109 return endResult;
110 }
111 }