View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package com.github.dexecutor.executor.support;
19  
20  /**
21   * 
22   * @author Nadeem Mohammad
23   *
24   */
25  public final class ThreadPoolUtil {
26  	
27  	private ThreadPoolUtil() {
28  		
29  	}
30  	/**
31  	 * Each tasks blocks 90% of the time, and works only 10% of its
32  	 *	lifetime. That is, I/O intensive pool
33  	 * @return io intesive Thread pool size
34  	 */
35  	public static int ioIntesivePoolSize() {
36  		
37  		double blockingCoefficient = 0.9;
38  		return poolSize(blockingCoefficient);
39  	}
40  
41  	/**
42  	 * 
43  	 * Number of threads = Number of Available Cores / (1 - Blocking
44  	 * Coefficient) where the blocking coefficient is between 0 and 1.
45  	 * 
46  	 * A computation-intensive task has a blocking coefficient of 0, whereas an
47  	 * IO-intensive task has a value close to 1,
48  	 * so we don't have to worry about the value reaching 1.
49  	 *  @param blockingCoefficient 
50  	 *  @return Thread pool size
51  	 */
52  	public static int poolSize(double blockingCoefficient) {
53  		int numberOfCores = Runtime.getRuntime().availableProcessors();
54  		int poolSize = (int) (numberOfCores / (1 - blockingCoefficient));
55  		return poolSize;
56  	}
57  }