排行榜前十名,seo 专业,免费做网站支持绑定,交易网站建设具体方案点击关注公众号#xff0c;利用碎片时间学习 有一个大List集合#xff0c;遍历进行一些耗时操作#xff0c;不能达到性能要求#xff0c;查询日志#xff0c;单个任务虽然有不少数据库和第三方API请求#xff0c;比较耗时#xff0c;但返回效率尚可#xff0c;所以优先… 点击关注公众号利用碎片时间学习 有一个大List集合遍历进行一些耗时操作不能达到性能要求查询日志单个任务虽然有不少数据库和第三方API请求比较耗时但返回效率尚可所以优先采用多线程方式进行处理并行请求数据库和第三方API因为处理完还要对list所属的数据进行操作所以线程池多线程处理要等待全部处理完。 相关的代码如下 Test
public void testTB()
{List String list new ArrayList ();for(int i 0; i 900; i){list.add(a);}ExecutorService touchWorker Executors.newFixedThreadPool(4, new ThreadFactoryBuilder().setNameFormat(touch-send-worker-%d).build());int size list.size();if(size 100){int batch size % 100 0 ? size / 100 : size / 100 1;for(int j 0; j batch; j){int end (j 1) * 100;if(end size){end size;}List String subList list.subList(j * 100, end);touchWorker.execute(() - sleepTest(subList));}touchWorker.shutdown();while(true){if(touchWorker.isTerminated()){break;}}}else{sleepTest(list);}
}
private void sleepTest(List String subList)
{for(String i: subList){try{//耗时操作System.out.println(###### i ###### Thread.currentThread().getName());// Thread.sleep(1000*30);}catch(Exception e){e.printStackTrace();}}
} void shutdown() 启动一次顺序关闭执行以前提交的任务但不接受新任务。若已经关闭则调用没有其他作用。 抛出SecurityException - 如果安全管理器存在并且关闭此 ExecutorService 可能操作某些不允许调用者修改的线程因为它没有保持RuntimePermission(modifyThread)或者安全管理器的 checkAccess 方法拒绝访问。 boolean isTerminated() 若关闭后所有任务都已完成则返回true。注意除非首先调用shutdown或shutdownNow否则isTerminated永不为true。返回若关闭后所有任务都已完成则返回true。 当然还有一种方法是之前写的方法比上边的臃肿了不过会获取返回结果进行处理逻辑是获取所有页面的List多线程遍历List后将所有页面的违规词查出发送邮件代码 /*** 落地页违规词排查(多线程)。* return*/
Test
public void getViolationsLandpageByThreadPool() {try {landPageService.getViolationsLandpageByThreadPool(1年);} catch (Exception e) {e.printStackTrace();}
}// 开始时间
long start System.currentTimeMillis();
/*ListLandPage list new ArrayListLandPage();
for (int i 1; i 3000; i) {list.add(i );
}*/ListLandPage list landPageDao.getPageIdAndPath();
// 初始化敏感词库对象
SensitiveWordInit sensitiveWordInit new SensitiveWordInit();
// 从数据库中获取敏感词对象集合目前先放在资源文件中等以后比较多或者需要界面操作时再用数据库
// 构建敏感词库
Map sensitiveWordMap sensitiveWordInit.initKeyWord(vioKey);
// 传入SensitivewordEngine类中的敏感词库
SensitivewordEngine.sensitiveWordMap sensitiveWordMap;
// 每500条数据开启一条线程
int threadSize 11000;
// 总数据条数
int dataSize list.size();
// 线程数
int threadNum dataSize / threadSize 1;
// 定义标记,过滤threadNum为整数
boolean special dataSize % threadSize 0;/*list.parallelStream().forEach(url -{});*/
// 创建一个线程池
ExecutorService exec Executors.newFixedThreadPool(threadNum);
// 定义一个任务集合
ListCallableListLandPage tasks new ArrayListCallableListLandPage();
CallableListLandPage task null;
ListLandPage cutList null;// 确定每条线程的数据
for (int i 0; i threadNum; i) {if (i threadNum - 1) {if (special) {break;}cutList list.subList(threadSize * i, dataSize);} else {cutList list.subList(threadSize * i, threadSize * (i 1));}// System.out.println(第 (i 1) 组 cutList.toString());final ListLandPage listStr cutList;task new CallableListLandPage() {Overridepublic ListLandPage call() throws Exception {
// System.out.println(Thread.currentThread().getName() 线程 listStr.get(0).getPageId());ListLandPage result new ArrayListLandPage();for (LandPage landPage : listStr) {Long pageId landPage.getPageId();String path landPage.getPath();Integer version landPage.getVersion();String pageUrl landPage.getPageUrl();String actualUser landPage.getActualUser();Integer pageType landPage.getPageType();if (StringUtils.isNotBlank(path)) {// 调用第一个方法获取html字符串String html httpRequest(path);// 调用第二个方法获取包含的违规词if(StringUtils.isNotBlank(html)){html html.replaceAll(!--(.*?)--,);
// String buffer htmlFiter2(html);SetString bufferSet SensitivewordEngine.getSensitiveWord(html, 1);// 得到敏感词有哪些传入2表示获取所有敏感词//sensitiveWordFiltering(html);/*String[] word {备案,错过将延误,仅需1980元};for(int i0 ;iword.length;i){if(html.contains(word[i])){bufferSet.add(word[i]);}}*/String[] word {一年,1年学完,一年学完,1年内学完,一年内学完};for(int i0 ;iword.length;i){if(html.contains(word[i])){bufferSet.add(word[i]);}}if (null!bufferSetbufferSet.size()0) {String sensitiveWord bufferSet null ? null : bufferSet.toString();if ([].equals(sensitiveWord)){sensitiveWord ;}LandPage page new LandPage();page.setPageId(pageId);page.setPath(path);page.setVersion(version);page.setDescription(sensitiveWord);page.setPageUrl(pageUrl);page.setActualUser(actualUser);page.setPageType(pageType);result.add(page);System.out.println(pageUrl);}}}}return (ListLandPage) result;}};// 这里提交的任务容器列表和返回的Future列表存在顺序对应的关系tasks.add(task);
}ListFutureListLandPage results exec.invokeAll(tasks);
ListLandPage result new ArrayListLandPage();
for (FutureListLandPage future : results) {result.addAll(future.get());
}// 关闭线程池
exec.shutdown();
System.out.println(线程任务执行结束);
System.err.println(执行任务消耗了 (System.currentTimeMillis() - start) 毫秒);System.out.println(共有###########list.size() ); result就是需要发送邮件的相关结果了 来源blog.csdn.net/qililong88/article/details/114320641 推荐 最全的java面试题库 PS因为公众号平台更改了推送规则如果不想错过内容记得读完点一下“在看”加个“星标”这样每次新文章推送才会第一时间出现在你的订阅列表里。点“在看”支持我们吧