日期:2014-05-19 浏览次数:20632 次
public class CommentsThreadUtil { @Autowired private GetFeedCommentsService getFeedCommentsService; private static final Log LOGGER = LogFactory .getLog(CommentsThreadUtil.class); private static final int THREADNUM = Runtime.getRuntime() .availableProcessors() * 10; private static final long TIMEOUT = 100; ExecutorService threadPool = Executors.newFixedThreadPool(THREADNUM); @Profiled public void excute(int host, List<MatterFeedData> matterFeedDatas, ProcessMatterCommentBuilder processMatterCommentBuilder) { for (MatterFeedData matterFeedData : matterFeedDatas) { threadPool.execute(new SimpleThread(host, matterFeedData, processMatterCommentBuilder)); } try { if (!threadPool.awaitTermination(TIMEOUT, TimeUnit.MILLISECONDS)) { if (LOGGER.isInfoEnabled()) { LOGGER.info("timeout:" + host + ",matterFeedData=" + matterFeedDatas); } } } catch (InterruptedException e) { // threadPool.shutdownNow(); LOGGER.error("取评论线程池执行出错:host=" + host, e); } } class SimpleThread implements Runnable { private int host; private MatterFeedData matterFeedData; private ProcessMatterCommentBuilder processMatterCommentBuilder; public SimpleThread(int host, MatterFeedData matterFeedData, ProcessMatterCommentBuilder processMatterCommentBuilder) { this.host = host; this.matterFeedData = matterFeedData; this.processMatterCommentBuilder = processMatterCommentBuilder; } @Override public void run() { List<MatterComment> matterComments = getFeedCommentsService .getFeedComments(host, matterFeedData.getUgcInfo(), processMatterCommentBuilder); matterFeedData.setMatterComments(matterComments); } } }