import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.*;
/**
* <b>DESCRIPTION:</b>线程池工具类,可以根据自己的业务生成自己的线程池
* 具体配置参考properties/threadpool.properties配置文件<br/>
*/
public class CommonThreadPoolUtil {
private static final String THREAD_POOL_FILE = "properties/threadpool.properties";
private static final String DEFAULT_PREFIX = "default";
private static Map<String, ExecutorService> threadMap = new ConcurrentHashMap<String, ExecutorService>();
private static final RejectedExecutionHandler HANDLER = new ThreadPoolExecutor.CallerRunsPolicy();
/**
* @Date 2019/5/11 13:40
* @Param * @param preFix
* @Return java.util.concurrent.ExecutorService
* 创建线程池,参数线程配置前缀
*/
public static ExecutorService createThreadPool(String preFix) throws Exception {
preFix = preFix == null ? DEFAULT_PREFIX : preFix;
Properties config = new Properties();
config.load(CommonThreadPoolUtil.class.getClassLoader().getResourceAsStream(THREAD_POOL_FILE));
ThreadModel threadModel = new ThreadModel();
BeanInfo beanInfo = Introspector.getBeanInfo(threadModel.getClass());
PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors();
for(String key : config.stringPropertyNames()) {
if (key.startsWith(preFix)){
String fieldName = key.substring(preFix.length()+1);
for (PropertyDescriptor property : properties) {
if (property.getName().equals(fieldName)) {
property.getWriteMethod().invoke(threadModel,Integer.parseInt(config.getProperty(key)));
}
}
}
}
return createExecutorService(threadModel);
}
public static ExecutorService getExecuteThreadTask(String preFix) {
preFix = preFix == null ? DEFAULT_PREFIX : preFix;
return threadMap.get(preFix);
}
public static ExecutorService executeThreadTask(Runnable runnable) throws Exception {
ExecutorService executorService = threadMap.get(DEFAULT_PREFIX);
if (executorService == null) {
executorService = createThreadPool(null);
threadMap.put(DEFAULT_PREFIX, executorService);
}
executorService.execute(runnable);
return executorService;
}
/**
* @Date 2019/5/11 13:41
* @Param * @param runnable 执行线程
* @param prefix 线程配置前缀
* @Return java.util.concurrent.ExecutorService
*
*/
public static ExecutorService executeThreadTask(Runnable runnable,String prefix) throws Exception {
ExecutorService executorService = threadMap.get(prefix);
if (executorService == null) {
executorService = createThreadPool(prefix);
threadMap.put(prefix,executorService);
}
executorService.execute(runnable);
return executorService;
}
private static ExecutorService createExecutorService(ThreadModel threadModel) {
ExecutorService executorService = new ThreadPoolExecutor(threadModel.getMinSize(),
threadModel.getMaxSize(), threadModel.getKeepAliveTime(), TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(threadModel.getWorkQueueSize()),
HANDLER);
return executorService;
}
/**
* @Author 张凯
* @Date 2019/5/11 13:42
* @Param * @param callable 执行线程
* @param prefix 线程配置前缀
* @Return java.util.concurrent.Future
*
*/
public static Future executeThreadTask(Callable callable,String prefix) throws Exception {
ExecutorService executorService = threadMap.get(prefix);
if (executorService == null) {
executorService = createThreadPool(prefix);
threadMap.put(prefix,executorService);
}
Future future = executorService.submit(callable);
return future;
}
}
java线程池工具类
最新推荐文章于 2024-10-02 06:00:34 发布