View Javadoc

1   package org.apache.maven.lifecycle.internal;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * @since 3.0
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       * Returns the thread count to use or null for unlimited threads.
77       *
78       * @param threadCountConfiguration The property passed from the command line.
79       * @param perCoreThreadCount       Indicates if the threa count should be scaled per cpu core.
80       * @param largestBuildListSize     the size of the largest module list (the number of modules)
81       * @return The number of threads to use or null if unlimited
82       */
83  
84      Integer getThreadCount( String threadCountConfiguration, boolean perCoreThreadCount, int largestBuildListSize )
85      {
86          // Default to a value that is not larger than what we can use ;)
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 }