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      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       * Returns the thread count to use or null for unlimited threads.
76       *
77       * @param threadCountConfiguration The property passed from the command line.
78       * @param perCoreThreadCount       Indicates if the threa count should be scaled per cpu core.
79       * @param largestBuildListSize     the size of the largest module list (the number of modules)
80       * @return The number of threads to use or null if unlimited
81       */
82  
83      Integer getThreadCount( String threadCountConfiguration, boolean perCoreThreadCount, int largestBuildListSize )
84      {
85          // Default to a value that is not larger than what we can use ;)
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 }