<bean id="threadPoolTaskExecutor"
class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<!-- 核心线程数,默认为1 -->
<property name="corePoolSize" value="5" /> <!-- 最大线程数,默认为Integer.MAX_VALUE -->
<property name="maxPoolSize" value="10" /> <!-- 队列最大长度,一般需要设置值>=notifyScheduledMainExecutor.maxNum;默认为Integer.MAX_VALUE
<property name="queueCapacity" value="1000" /> --> <!-- 线程池维护线程所允许的空闲时间,默认为60s -->
<property name="keepAliveSeconds" value="300" /> <!-- 线程池对拒绝任务(无线程可用)的处理策略,目前只支持AbortPolicy、CallerRunsPolicy;默认为后者 -->
<property name="rejectedExecutionHandler">
<!-- AbortPolicy:直接抛出java.util.concurrent.RejectedExecutionException异常 -->
<!-- CallerRunsPolicy:主线程直接执行该任务,执行完之后尝试添加下一个任务到线程池中,可以有效降低向线程池内添加任务的速度 -->
<!-- DiscardOldestPolicy:抛弃旧的任务、暂不支持;会导致被丢弃的任务无法再次被执行 -->
<!-- DiscardPolicy:抛弃当前任务、暂不支持;会导致被丢弃的任务无法再次被执行 -->
<bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" />
</property>
</bean>

然后定义一个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<Object[]>> { private AcLogCounterWebMapper acLogCounterWebMapper;
private List<PJournal> journalList;
private String accountId;
private String startTime;
private String endTime;
private List<String> dateHeader; public void setAcLogCounterWebMapper(AcLogCounterWebMapper acLogCounterWebMapper) {
this.acLogCounterWebMapper = acLogCounterWebMapper;
} public void setJournalList(List<PJournal> 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<String> dateHeader) {
this.dateHeader = dateHeader;
} @Override
public ArrayList<Object[]> call() throws Exception {
ArrayList<Object[]> rows = new ArrayList<>();
// 根据机构ID 刊ID 年月 查询对应的记录
for (PJournal journal : journalList) {
String journalId = journal.getMdcProJournalId();
// 以 年月 TYPE 分类
List<Map<String, String>> list = acLogCounterWebMapper.getLogCounterJr1(accountId, journalId, startTime,
endTime); List<String> 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<String, String> mapC = new HashMap<String, String>(); if (list.isEmpty()) {
// 未查询出记录则直接赋值为0
content.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<String, String> map : list) {
// 月份
String time = (String) map.get("LOGTIME");
// 类型 3 4
String 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<Future<ArrayList<Object[]>>> results = new ArrayList<Future<ArrayList<Object[]>>>(10);
List<List<PJournal>> splitList = ListUtils.averageAssign(journalList, 10);
for (List<PJournal> 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<ArrayList<Object[]>> future = threadPoolTaskExecutor.submit(ct);
results.add(future);
} for (Future<ArrayList<Object[]>> future : results) {
ArrayList<Object[]> arrayList = future.get();
rows.addAll(arrayList);
}

Spring中线程池的使用的更多相关文章

  1. Spring中线程池的应用

    多线程并发处理起来通常比较麻烦,如果你使用spring容器来管理业务bean,事情就好办了多了.spring封装了Java的多线程的实现,你只需要关注于并发事物的流程以及一些并发负载量等特性,具体来说 ...

  2. Spring Boot 线程池

    参考 SpringBoot 线程池 程序猿DD-Spring Boot使用@Async实现异步调用:自定义线程池 如何优雅的使用和理解线程池 Spring Boot线程池的使用心得 博客园-Sprin ...

  3. Java中java.util.concurrent包下的4中线程池代码示例

    先来看下ThreadPool的类结构 其中红色框住的是常用的接口和类(图片来自:https://blog.csdn.net/panweiwei1994/article/details/78617117 ...

  4. Spring集成线程池

    自己在程序中手动New很容易造成线程滥用,创建线程也是比较消耗资源的操作,所以建议如果有此需求,将线程池统一交给Spring框架进行管理. 如下: <!--Spring 集成线程池,不允许自己开 ...

  5. spring @Async 线程池使用

    最近公司项目正逐渐从dubbo向springCloud转型,在本次新开发的需求中,全部使用springcloud进行,在使用时线程池,考虑使用spring封装的线程池,现将本次使用心得及内容记录下来 ...

  6. Java中线程池,你真的会用吗?

    在<深入源码分析Java线程池的实现原理>这篇文章中,我们介绍过了Java中线程池的常见用法以及基本原理. 在文中有这样一段描述: 可以通过Executors静态工厂构建线程池,但一般不建 ...

  7. Java并发编程中线程池源码分析及使用

    当Java处理高并发的时候,线程数量特别的多的时候,而且每个线程都是执行很短的时间就结束了,频繁创建线程和销毁线程需要占用很多系统的资源和时间,会降低系统的工作效率. 参考http://www.cnb ...

  8. 沉淀再出发:java中线程池解析

    沉淀再出发:java中线程池解析 一.前言 在多线程执行的环境之中,如果线程执行的时间短但是启动的线程又非常多,线程运转的时间基本上浪费在了创建和销毁上面,因此有没有一种方式能够让一个线程执行完自己的 ...

  9. Java中线程池,你真的会用吗?ExecutorService ThreadPoolExcutor

    原文:https://www.hollischuang.com/archives/2888 在<深入源码分析Java线程池的实现原理>这篇文章中,我们介绍过了Java中线程池的常见用法以及 ...

随机推荐

  1. python使用C扩展

    CPython还为开发者实现了一个有趣的特性,使用Python可以轻松调用C代码 开发者有三种方法可以在自己的Python代码中来调用C编写的函数-ctypes,SWIG,Python/C API.每 ...

  2. morhpia(4)-更新

    更新由2部分组成:一个查询和一组更新操作符.本例是跟所有薪水小于等于2000的员工涨工资500. @Test public void update() throws Exception { //第一步 ...

  3. 106 Construct Binary Tree from Inorder and Postorder Traversal 从中序与后序遍历序列构造二叉树

    给定一棵树的中序遍历与后序遍历,依据此构造二叉树.注意:你可以假设树中没有重复的元素.例如,给出中序遍历 = [9,3,15,20,7]后序遍历 = [9,15,7,20,3]返回如下的二叉树:    ...

  4. MySql下载地址

    因为下载mysql需要注册,很麻烦,记录下下载地址: My sql 5.1.71 http://cdn.mysql.com/Downloads/MySQL-5.1/mysql-5.1.71-win32 ...

  5. Android adb命令,linux中各种命令

    常用的ADB命令 1. 显示系统中全部Android平台: android list targets 2. 显示系统中全部AVD(模拟器): android list avd 3. 创建AVD(模拟器 ...

  6. Yii2.0数据格式器

    平时我们在写代码中,总是要写一个单独的文件来全局处理常用的数据格式.Yii2.0却很人性化,为我们内置了一套数据格式器. 1.格式化日期和时间 Yii::$app->formatter-> ...

  7. GoAccess参数选项

    GoAccess - 1.2 Usage: goaccess [filename] [ options ... ] [-c][-M][-H][-q][-d][...]The following opt ...

  8. 如何通过Xcode 5中集成的XCTest框架进行简单的单元测试

    XCTest 1.第一个单元测试 XCTest是Xcode 5中自带的测试框架 下面从一个Demo开始.首先用Xcode新建一个工程UnitTestDemo,工程目录结构如下: 可以看到工程下面多了一 ...

  9. 洛谷 P2068 统计和

    题目描述 给定一个长度为n(n<=100000),初始值都为0的序列,x(x<=10000)次的修改某些位置上的数字,每次加上一个数,然后提出y (y<=10000)个问题,求每段区 ...

  10. sublime text 3中emmet常用技巧

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...