Skip to content

Instantly share code, notes, and snippets.

@Coneboy-k
Last active February 6, 2018 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Coneboy-k/5e8c16baf39d2f0f35842a9662a61990 to your computer and use it in GitHub Desktop.
Save Coneboy-k/5e8c16baf39d2f0f35842a9662a61990 to your computer and use it in GitHub Desktop.
KKThreadHelp
/**
* Created by JackSun on 2017/5/25.
*
* Connect Me. admin@jacksun.me
*/
public class KKThreadHelp {
// 计算类型
public enum CALCULATE_TYPE {
IO, // IO 密集型
CALCULAT // 计算密集型
}
// CPU 核心线程数
private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
// 最小线程数据
public final static int CORE_POOL_SIZE = Runtime.getRuntime().availableProcessors() * 1;
public final static long KEEP_ALIVE_TIME = 90L;
/**
* 根据类型获取最大线程数, 参考这个文章
*
* <p>http://www.blogjava.net/bolo/archive/2015/01/20/422296.html</>
*/
public final static int maxPoolSize(CALCULATE_TYPE calculateType) {
if (calculateType == null) {
return CPU_COUNT * 2 + 1;
}
int maxPoolSize;
switch (calculateType) {
case IO: {
maxPoolSize = CORE_POOL_SIZE * 2 < 64 ? 64 : CORE_POOL_SIZE * 2;
break;
}
case CALCULAT:
default: {
maxPoolSize = CORE_POOL_SIZE * 2 + 1;
break;
}
}
return maxPoolSize;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment