package com.tuchaoshi.base.concurrent; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.Executors; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.common.collect.Lists; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListeningExecutorService; import com.google.common.util.concurrent.MoreExecutors; public class ForkJoinPool<T> { private final static Logger logger = LoggerFactory .getLogger(ForkJoinPool.class); public static final int AVAILABLE_PROCESSORS_SIZE = Runtime.getRuntime() .availableProcessors(); private ListeningExecutorService executorService = null; private ThreadLocal<List<ListenableFuture<T>>> futuresThreadLocal = new ThreadLocal<List<ListenableFuture<T>>>(){ protected java.util.List<com.google.common.util.concurrent.ListenableFuture<T>> initialValue() { return Lists.newArrayList(); }; }; public ForkJoinPool() { this(AVAILABLE_PROCESSORS_SIZE*2); } public ForkJoinPool(int poolSize) { executorService = MoreExecutors .listeningDecorator(Executors .newFixedThreadPool(poolSize)); } public void createTask() { } /** * * @description * @return ListenableFuture<T> * @Exception */ public ForkJoinPool<T> addTaskList(final List<Callable<T>> callables) { if(callables!=null){ for(Callable<T> c:callables){ addTask(c); } } return this; } /** * * @description * @return ListenableFuture<T> * @Exception */ public ForkJoinPool<T> addTask(final Callable<T> callable) { ListenableFuture<T> listenableFuture = executorService.submit(callable); futuresThreadLocal.get().add(listenableFuture); return this; } /** * 多线程执行商品生成信息 * * @description * @return * @Exception */ public List<T> executeTask(List<ListenableFuture<T>> futures) { long gstartTime = System.currentTimeMillis(); ListenableFuture<List<T>> successfulQueries = Futures .successfulAsList(futures); try { // 获取所有线程的执行结果 List<T> lists = successfulQueries.get(); return lists; } catch (Exception e) { logger.error(e.getMessage(), e); } logger.info(" executeTask ! cost time:" + (System.currentTimeMillis() - gstartTime)); return null; } /** * 多线程执行商品生成信息 * * @description * @return * @Exception */ public List<T> executeTask() { List<ListenableFuture<T>> futures = futuresThreadLocal.get(); try { return executeTask(futures); } catch (Exception e) { logger.error(e.getMessage(), e); } finally { futuresThreadLocal.remove(); } return null; } /** * 拆分任务 * * @param tasks * @param 拆分数量 * @return */ public static <T> List<T> mergeTask(List<List<T>> tasks) { if(tasks==null){ return null; } List<T> list = Lists.newArrayList(); for(List<T> l:tasks){ if(l!=null){ list.addAll(l); } } return list; } /** * 拆分任务 * * @param tasks * @param 拆分数量 * @return */ public static <T> List<List<T>> splitTask(List<T> tasks, Integer taskSize) { List<List<T>> list = Lists.newArrayList(); if(tasks==null || taskSize <= 0){ return list; } if(tasks.size() < taskSize){ list.add(tasks); return list; } int baseNum = tasks.size() / taskSize; // 每个list的最小size int remNum = tasks.size() % taskSize; // 得到余数 int index = 0; for (int i = 0; i < taskSize; i++) { int arrNum = baseNum; // 每个list对应的size if (i < remNum) { arrNum += 1; } List<T> ls = Lists.newArrayList(); for (int j = index; j < arrNum + index; j++) { ls.add(tasks.get(j)); } list.add(ls); index += arrNum; } return list; } public void shutdown() { this.executorService.shutdown(); } }
相关推荐
C# 自定义控件 自定义ComboBox。其他控件的自定义与此类似。
QT自定义窗口 自由拖动 自定义标题
tablayout+viewpager自定义tab和自定义指示器。完美解决滑动卡顿。
用户自定义控件,用户自定义控件,用户自定义控件,用户自定义控件,用户自定义控件,用户自定义控件,用户自定义控件,用户自定义控件,用户自定义控件用户自定义控件,用户自定义控件,用户自定义控件,用户自定义...
springboot工程通过自定义response注解、java反射机制、自定义java拦截器、自定义功能类实现WebMvcConfigurer接口等功能,实现自定义规范化返回数据结构。
vb.net 自定义控件 自定义属性 UITypeEditor UI 类型编辑器 实例 提供一个示例 UITypeEditor,它使用 IWindowsFormsEditorService 显示用于用户输入的 Form。 IWindowsFormsEditorService 只能通过 PropertyGrid ...
自定义按钮自定义按钮自定义按钮自定义按钮自定义按钮
自定义导航模板
1.包含自定义消息 2.包含Panel类型的自定义插件 3.包含Display类型的自定义插件
这是我用C#写的自定义组件和控件,内附一个testForm来演示如何使用。 其中,自定义的组件有:速选组件、窗口靠边停靠组件、窗口抖动组件。 自定义控件有:黑白的菜单栏、带进度显示的进度条等。
自定义文件格式 自定义文件格式 自定义文件格式 自定义文件格式 自定义文件格式
Android自定义中国象棋,原创-------转载注明出处; Android自定义中国象棋,原创-------转载注明出处; Android自定义中国象棋,原创-------转载注明出处; Android自定义中国象棋,原创-------转载注明出处; ...
FreeSwitch完整的自定义模块定义和改善自定义事件的例子,详细介绍见博客:https://blog.csdn.net/xxm524/article/details/126211171
java自定义标签java自定义标签java自定义标签java自定义标签java自定义标签java自定义标签
自定义分页标签自定义分页标签自定义分页标签自定义分页标签自定义分页标签自定义分页标签自定义分页标签自定义分页标签自定义分页标签自定义分页标签自定义分页标签自定义分页标签自定义分页标签自定义分页标签...
介绍了mybatis自定义标签,添加自定义标签的实现代码,通过实例代码展示给大家
uni-app自定义弹窗组件指令部分,支持自定义图片,文本、按钮等功能。
sql自定义函数 sql自定义函数 sql自定义函数
JAVA动态表单设计,自定义表单,自定义数据,在线设计,数据库存储
微信小程序源码(含截图)自定义tabbar微信小程序源码(含截图)自定义tabbar微信小程序源码(含截图)自定义tabbar微信小程序源码(含截图)自定义tabbar微信小程序源码(含截图)自定义tabbar微信小程序源码(含...