Spring中线程池的使用

然后定义一个component组件,然后线程的引用就十分简单了,只要把这个线程扔进这个线程池子就行了

package com.digitalpublishing.sage.service.impl;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;import org.springframework.stereotype.Component;import com.digitalpublishing.module.sage.PJournal;
import com.digitalpublishing.sage.dao.AcLogCounterWebMapper;/** 
* @ClassName: CounterWebJr1 
* @Description: webJr1统计数据查询
* @author wd
* @date 2018年12月26日 上午9:22:20 
*  
*/
@Component
public class CounterWebJr1 implements Callable< ArrayList> {private AcLogCounterWebMapper acLogCounterWebMapper;private List journalList;private String accountId;private String startTime;private String endTime;private List dateHeader;public void setAcLogCounterWebMapper(AcLogCounterWebMapper acLogCounterWebMapper) {this.acLogCounterWebMapper = acLogCounterWebMapper;}public void setJournalList(List journalList) {this.journalList = journalList;}public void setAccountId(String accountId) {this.accountId = accountId;}public void setStartTime(String startTime) {this.startTime = startTime;}public void setEndTime(String endTime) {this.endTime = endTime;}public void setDateHeader(List dateHeader) {this.dateHeader = dateHeader;}@Overridepublic  ArrayList call() throws Exception {ArrayList rows = new ArrayList<>();// 根据机构ID 刊ID 年月 查询对应的记录for (PJournal journal : journalList) {String journalId = journal.getMdcProJournalId();// 以 年月 TYPE 分类List> list = acLogCounterWebMapper.getLogCounterJr1(accountId, journalId, startTime,endTime);List content = new ArrayList<>();content.add(journal.getTitle());content.add("SAGE Publications");content.add("SAGE Journals");content.add("10.1177/" + journal.getFla());content.add(journal.getFla());content.add(journal.getIssn());content.add(journal.getEissn());int total = 0;// 一个刊统计时间段内的总数int html = 0;// 一个刊统计时间段内的html总数int pdf = 0;// 一个刊统计时间段内的pdf总数// 每个刊每个月的访问数Map mapC = new HashMap();if (list.isEmpty()) {// 未查询出记录则直接赋值为0content.add(String.valueOf(total));content.add(String.valueOf(html));content.add(String.valueOf(pdf));for (int i = 0; i < dateHeader.size(); i++) {content.add("0");}} else {// 有记录的可能会存在一个月内不同TYPE的两条记录for (Map map : list) {// 月份String time = (String) map.get("LOGTIME");// 类型 3 4String type = map.get("TYPE");// 访问数String num = map.get("C");// 刊 时间段内访问总数total += Integer.valueOf(num);// 刊时间段内 PDF 和 HTML 访问数 && PDF,HTML所有访问数if ("3".equals(type)) {pdf += Integer.valueOf(num);}if ("4".equals(type)) {html += Integer.valueOf(num);}if (mapC.containsKey(time)) {String val = mapC.get(time);mapC.put(time, String.valueOf(Integer.valueOf(val) + Integer.valueOf(num)));} else {mapC.put(time, num);}}content.add(String.valueOf(total));content.add(String.valueOf(html));content.add(String.valueOf(pdf));for (String key : dateHeader) {if (mapC.containsKey(key)) {content.add(mapC.get(key));} else {content.add("0");}}}rows.add(content.toArray());}return rows;}}

最后在你所需要的地方就可以调用这个组件了,不论是service还是controller都行

List>> results = new ArrayList>>(10);
List> splitList = ListUtils.averageAssign(journalList, 10);
for (List list : splitList) {CounterWebJr1 ct = new CounterWebJr1();ct.setAcLogCounterWebMapper(acLogCounterWebMapper);ct.setAccountId(accountId);ct.setDateHeader(dateHeader);ct.setStartTime(startTime);ct.setEndTime(endTime);ct.setJournalList(list);Future> future = threadPoolTaskExecutor.submit(ct);results.add(future);
}for (Future> future : results) {ArrayList arrayList = future.get();rows.addAll(arrayList);
}

转载于:https://www.cnblogs.com/miye/p/9870828.html


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部